Asp mvc using dashes in controller and routing names?

I have two questions. I am new to MVC and love the new way to customize the controller and views, but I cannot figure out how to do the following:

1) enter a url e.g. www.homepage.com/coming-soon

for this type of url, how to do it right? do you create a controller named ComingSoonController and somehow magically insert a dash through routing? Note that I do NOT want to emphasize that this is not in the interest of SEO. or will any action name soon appear on some other controller that is not in the URL and use the [ActionName ("dash-name") attribute?

2) facebook, linkedin and twitter have URLs such as www.facebook.com/[profile name]. How will this be done in MVC? obviously [profile name] is dynamic. and the code, obviously, will live in a controller called, say, profiles. so it seems to me that you will need to make MVC smart enough to know when this second part of the url is the profile name and NOT the controller, and direct it to the correct action on the profile controller? is it easier than it seems?

+4
source share
3 answers

1) It depends on whether this is a dynamic part soon or not. I assume this will suggest something like this:

Global.asax

public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute( "Page", // Route name "{pageName}", // URL with parameters new { controller = "Home", action = "Page"} // Parameter defaults ); } public class HomeController : Controller { public ActionResult Page(string pageName) { return View(); } } 

2) You can solve the same way as I showed above, but keep in mind that the order of the routes is important. And this is the first thing that corresponds to victory. If you want the two actions to have different logic, but the same structure of the URL www.mysite.com/coming-soon and www.mysite.com/{profile name}, assuming that the first url has a static part, and the later dynamics you can do something like this:

Global.asax

 public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute( "Coming-soon", // Route name "coming-soon", // URL with parameters new { controller = "Home", action = "ComingSoon" } // Parameter defaults ); routes.MapRoute( "Profiles", // Route name "{profileName}", // URL with parameters new { controller = "Home", action = "Profile"} // Parameter defaults ); } public class HomeController : Controller { public ActionResult ComingSoon() { return View(); } public ActionResult Profile(string profileName) { return View(); } } 
+3
source

You can create your own route handler to allow hyphens in URLs:

Create a new handler

 public class HyphenatedRouteHandler : MvcRouteHandler{ protected override IHttpHandler GetHttpHandler(RequestContext requestContext) { requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString().Replace("-", "_"); requestContext.RouteData.Values["action"] = requestContext.RouteData.Values["action"].ToString().Replace("-", "_"); return base.GetHttpHandler(requestContext); } } 

... and a new route:

 routes.Add( new Route("{controller}/{action}/{id}", new RouteValueDictionary( new { controller = "Default", action = "Index", id = "" }), new HyphenatedRouteHandler()) ); 

MVC Fixed URLs

+3
source

I developed the NuGet open source library for the first issue, which implicitly converts EveryMvc / Url to every-mvc / url.

Dotted URLs are much more SEO friendly and easier to read. ( More on my blog post )

NuGet Package: https://www.nuget.org/packages/LowercaseDashedRoute/

To install it, simply open the NuGet window in Visual Studio by right-clicking Project and selecting NuGet Package Manager, and on the Online tab, enter β€œLine-hatched route” and it should appear.

Alternatively, you can run this code in the package manager console:

Install-Package LowercaseDashedRoute

After that, you should open App_Start / RouteConfig.cs and comment on the existing route.MapRoute (...) and add instead:

 routes.Add(new LowercaseDashedRoute("{controller}/{action}/{id}", new RouteValueDictionary( new { controller = "Home", action = "Index", id = UrlParameter.Optional }), new DashedRouteHandler() ) ); 

What is it. All URLs are lowercase, dotted, and converted implicitly if you do nothing.

Open Source Project Url: https://github.com/AtaS/lowercase-dashed-route

When it comes to the second problem, you can do this either by creating your own routes, or by processing not found using the error handling mechanism, but routing will be faster if you restrict the profile URLs to accept some rules (for example, not have no slashes) so that you can distinguish it from other URLs much easier, for example, from the URLs of content files, i.e. .css.js, etc.

+3
source

Source: https://habr.com/ru/post/1336273/


All Articles