开发者

Avoiding the Controller with Routing Rules in ASP.NET MVC

开发者 https://www.devze.com 2022-12-23 15:39 出处:网络
I\'ve created a website with ASP.NET MVC. I have a number of static pages that I am currently serving through a single controller called Home. This creates some rather ugly URLs.

I've created a website with ASP.NET MVC. I have a number of static pages that I am currently serving through a single controller called Home. This creates some rather ugly URLs.

example.com/Home/About 
example.com开发者_C百科/Home/ContactUs 
example.com/Home/Features

You get the idea. I'd rather not have to create a controller for each one of these as the actions simply call the View with no model being passed in.

Is there a way to write a routing rule that will remove the controller from the URL? I'd like it to look like:

example.com/About
example.com/ContactUs
example.com/Features

If not, how is this situation normally handled? I imagine I'm not the first person to run in to this.


Here's what I've done previously, using a constraint to make sure the shortcuts don't conflict with other routing rules:

routes.MapRoute(
    "HomeShortcuts",
    "{action}",
    new { controller = "Home", action = "Index" },
    new { action = "Index|About|ContactUs|Features" }
);


Add defaults for the controller names in the new statement. You don't have to have {controller} in the url.

0

精彩评论

暂无评论...
验证码 换一张
取 消