mvc roslyn_T4MVC和R4MVC-用于ASP.NET Core标记帮助器的Roslyn代码生成器

mvc roslyn

mvc roslyn

I've always loved the T4 text generator within Visual Studio. If you are looking for T4 within Visual Studio 2017 you need to install the "Visual Studio extension development" option within the installer to access it. However, T4 development seems stalled/done and if you want to utilize some of it.

我一直很喜欢Visual Studio中T4文本生成器。 如果要在Visual Studio 2017中寻找T4,则需要在安装程序中安装“ Visual Studio扩展开发”选项才能访问它。 但是,T4开发似乎停滞/完成,如果您想利用其中的一些。

There's a nice open source project called T4MVC that you can use with Visual Studio 2015 and ASP.NET MVC to create strongly typed helpers that eliminate the use of literal strings in many places. That means instead of:

有一个很好的开源项目T4MVC,您可以将其与Visual Studio 2015和ASP.NET MVC一起使用,以创建强类型的帮助程序,从而避免在许多地方使用文字字符串。 那意味着代替:

@Html.ActionLink("Dinner Details", "Details", "Dinners", new { id = Model.DinnerID }, null)

T4MVC lets you write

T4MVC让您编写

@Html.ActionLink("Dinner Details", MVC.Dinners.Details(Model.DinnerID))

Fast forward to 2017 and that team is working on a new project called R4MVC...it's a code generator that's based on Roslyn, the .NET Compiler Platform (hence the R).

快进到2017年,该团队正在研究一个名为R4MVC的新项目...这是一个基于Roslyn (.NET编译器平台,因此称为R)的代码生成器。

It also lets you update your @Html.ActionLinks to be strongly typed, but more importantly it lets you extend that to strongly typed taghelpers, so instead of:

它还可以让您将@ Html.ActionLinks更新为强类型,但更重要的是,您可以将其扩展到强类型的taghelpers,因此,您可以:

<a asp-action="Details" asp-controller="Dinners" asp-route-id="@Model.DinnerID">Dinner Details</a>

you can write

你可以写

<a mvc-action="MVC.Dinners.Details(Model.DinnerID)">Dinner Details</a>

It's generating the URL for that <a> tag using the method and parameter.

它使用方法和参数为该<a>标记生成URL。

Using an ASP.NET Core 1.1 app (2.0 is coming soon they say) I'll add the NuGet packages R4Mvc.Tools and R4Mvc, making sure to "include prerelease."

使用ASP.NET Core 1.1应用程序(他们说很快就会推出2.0),我将添加NuGet包R4Mvc.Tools和R4Mvc,确保“包括预发行版”。

Adding R4Mvc.Tools in NuGet

I'll run "Generate-R4MVC" in the Package Manager Console.

我将在程序包管理器控制台中运行“ Generate-R4MVC”。

Generate-R4MVC

There is a new R4Mvc.generated.cs file that gets created, and inside it is a whole bunch of classes based on the files on disk. For example I can type @Links.css, or @Links.lib and start getting intellisense for all my files on disk like JavaScript or CSS.

有一个新的R4Mvc.genic.cs文件被创建,并且在其中包含一堆基于磁盘上文件的类。 例如,我可以键入@ Links.css或@ Links.lib并开始对磁盘上的所有文件(如JavaScript或CSS)进行智能感知。

Links.css

When returning a view, rather than return View("About") I can do return View(Views.About):

返回视图时,可以返回View(Views.About),而不是返回View(“ About”):

return View(Views.About)

The R4MVC project also has Tag Helpers so their mvc-action attribute gives you strong typing like this:

R4MVC项目还具有Tag Helpers,因此它们的mvc-action属性为您提供了强大的输入,例如:

<a mvc-action="MVC.Home.Index()">

This R4MVC project is just getting started, but I'm sure they'd appreciate your support! Head over to https://github.com/T4MVC/R4MVC/issues and learn about what they are planning and perhaps help out!

这个R4MVC项目才刚刚开始,但是我相信他们会感谢您的支持! 前往https://github.com/T4MVC/R4MVC/issues ,了解他们正在计划什么,也许可以帮忙!

What do you think? Do you think there's value in smarter or strongly-typed URL generation with ASP.NET?

你怎么看? 您认为使用ASP.NET生成更智能或强类型的URL是否有价值?

翻译自: https://www.hanselman.com/blog/t4mvc-and-r4mvc-roslyn-code-generators-for-aspnet-core-tag-helpers

mvc roslyn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值