MVC6强类型的动作链接和视图

时间:2016-01-06 21:37:13

标签: c# asp.net-core-mvc

我们目前正在使用MVC6获得新的应用程序。在以前的版本中,我们使用了T4MVC,因此我们可以执行以下操作:

UIVisualEffectView

@Url.Action(MVC.Home.Index);

在新的应用程序中,我必须使用魔术字符串。我讨厌魔法字符串。对于MVC6,还有其他替代方案吗?

1 个答案:

答案 0 :(得分:5)

遇到GitHub上的这个AspNet.Mvc.TypedRouting存储库,当我最终转移到MVC6时,我觉得它很有用。

不确定它是否也处理视图

自述文件中的一些说明

要使用基于表达式的链接生成,您需要在Startup类中执行以下操作:

public void Configure(IApplicationBuilder app)
{
   // other configuration code

   app.UseMvc(routes =>
   {
        routes.UseTypedRouting();
   });
}

基本上,您可以执行以下操作:

// generating link without parameters - /Home/Index
urlHelper.Action<HomeController>(c => c.Index());

// generating link with parameters - /Home/Index/1
urlHelper.Action<HomeController>(c => c.Index(1));

// generating link with additional route values - /Home/Index/1?key=value
urlHelper.Action<HomeController>(c => c.Index(1), new { key = "value" });

// generating link where action needs parameters to be compiled, but you do not want to pass them - /Home/Index
// * With.No<TParameter>() is just expressive sugar, you can pass 'null' for reference types but it looks ugly
urlHelper.Action<HomeController>(c => c.Index(With.No<int>()));
相关问题