MVC6 Strongly typed links and action views

We are currently getting a new application that supports MVC6. In previous versions, we used T4MVC so that we could do things like:

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

and

 return View(MVC.Home.Views.Index, model); 

In the new application I have to use magic strings. I hate magic strings. Are there alternatives for MVC6?

+8
c # asp.net-core-mvc
source share
1 answer

Went through this AspNet.Mvc.TypedRouting Repository on GitHub, which I thought would be useful when I finally get to MVC6.

Not sure if it also handles views, but

Some instructions from the readme file

To use expression-based link generation, you need to do the following in your Startup class:

 public void Configure(IApplicationBuilder app) { // other configuration code app.UseMvc(routes => { routes.UseTypedRouting(); }); } 

Basically, you can do the following:

 // 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>())); 
+3
source share

All Articles