Asp.Net Routing - Display Full URL

I have a domain "http://www.abc.com". I deployed an ASP.net MVC4 application in this domain. I also configured the default route in RouteConfig.cs, as shown below.

routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "MyApp", action = "Home", id = UrlParameter.Optional } ); 

The above comparison ensures that anyone trying to visit "http://www.abc.com" automatically displays the page "http://www.abc.com/MyApp/Home"

Everything works as expected, but the address bar in the browser shows "http://www.abc.com" instead of "http://www.abc.com/MyApp/Home". Is there a way to get the browser to display the full URL, including the controller and action?

+4
source share
3 answers

One option: set the default route to a new controller, possibly called BaseController with a Root action:

 public class BaseController : Controller { public ActionResult Root() { return RedirectToAction("Home","MyApp"); } } 

and change your RouteConfig to what for root queries:

 routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Base", action = "Root", id = UrlParameter.Optional } ); 
+2
source

You will need to do some URL recycling. Probably the fastest way is to add a RewritePath call to your BeginRequest in Global.asax. In your case, it will be something like this:

 void Application_BeginRequest(Object sender, EventArgs e) { string originalPath = HttpContext.Current.Request.Path.ToLower(); if (originalPath == "/") //Or whatever is equal to the blank path Context.RewritePath("/MyApp/Home"); } 

An improvement will be to dynamically pull the URL from the route table for replacement. Or you can use Microsoft URL Rewrite , but this is a more complex IMO.

+2
source

Just remove the default options, the answer was given here:

How to force MVC to route to Home / Index instead of root?

0
source

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


All Articles