Asp.Net Routing - Display complete URL

asp net core routing multiple parameters
url routing in asp.net c#
attribute routes with the same name 'get' must have the same template
asp.net core dynamic routing
asp.net core endpoint routing
asp.net core mvc routing
asp.net core 3.0 endpoint routing
asp.net core 2.0 routing

I have a domain "http://www.abc.com". I have deployed an ASP.net MVC4 app on this domain. I have also configured a 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 mapping ensures that anyone attempting to visit "http://www.abc.com" is automatically shown the page for "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 any way to force the browser to show the complete URL including the controller and Action?

One option would be to set your default route to a new controller, maybe called BaseController with an action Root:

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

and modify your RouteConfig to point to that for root requests:

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

Display Full URL in mvc, Hello All, How can we display full URL in browser for mvc applications ? routes​.MapRoute( name: "default", template: "{controller}/{action}/{id  Routing is the process of directing an HTTP request to a controller and the functionality of this processing is implemented in System.Web.Routing. This assembly is not part of ASP.NET MVC. It is actually part of the ASP.NET runtime, and it was officially released with the ASP.NET as a .NET 3.5 SP1.

You'll need to do some kind of url rewriting. Probably the quickest way is to add a RewritePath call to your BeginRequest in Global.asax. In your case it'd 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 would be to dynamically pull the url from the route table for the replacement. Or you could use Microsoft URL Rewrite, but that's more complicated IMO.

Routing to controller actions in ASP.NET Core, NET Core MVC uses Routing Middleware to match URLs of incoming ASP.​NET Core controllers use the Routing middleware to match the URLs of See Routing for a full description of route templates and related options. is included in the sample download and is used to display routing information. Asp.Net Routing - Display complete URL. Ask Question Asked 7 years, 1 month ago. asp.net mvc url routing (disable access to /index and query string. 1.

Just remove the default parameters, it's been answered here:

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

URL Routing, NET 4.5 and Microsoft Visual Studio Express 2013 for We ASP.NET Routing Overview. URL routing allows you to configure an application to The links will be displayed as they were before the change, however the links  URL Routing is supported in .Net 3.5 SP1 or higher frameworks, if you want to use it in Visual Studio 2008 you will need to install .Net Framework 3.5 Service Pack 1. Adding Global.asax file to the project.

Routing in MVC, ASP.NET introduced Routing to eliminate needs of mapping each URL with a physical file. Routing enable us to define URL pattern that maps to the request  URL Rooting in ASP.NET (Web Forms) By Christos S. on December 15, 2013 • ( 5 ) If you are an ASP.NET MVC developer, you will certainly be aware of its default URL Routing behavior, where if for example you were to display a View named “Index” that was relying in a controller named “ProductsController”, you could request it with the following URL:

Routing and URL handling in MVC, The process of URL resolution in MVC is fundamentally different from Portal Engine. In this article, I'll explain the concepts and examples of MVC routing in natural Now that we've covered the whole routing and its components, let's take a look at If you wish to learn more about routes, navigate to ASP. An ASP.NET application which does not utilize routing maps incoming requests to actual files. It specifies the file and uses a query string value to select the displayed content. ASP.NET routing allows for the definition of URL patterns which map to request-handler files without the need to name specific files in the URL.

Generating Route URLs in ASP.NET Core MVC, This post describes how I use a custom RouteNames class together with UrlHelper.RouteUrl to generate route URLs in ASP.NET Core MVC. The complex segment logic was not updated in ASP.NET Core 3.0 routing performance update. Synchronous data access: Many complex apps have database access as part of their routing. ASP.NET Core 2.2 and earlier routing might not provide the right extensibility points to support database access routing.

Comments
  • This is not fair. because you need to do two server calls for one action rite ?
  • @Laurence - Your solution definitely works but I also can see VeeKeyBee's point. There has to be a more elegant solution using URL rewriting as suggested by Michael Yoon. Thanks for the great tip though. I might end up using this method if URL rewriting does not fit my needs
  • @VeeKeyBee I'm not sure what you mean by not fair. It's not ideal and does send the client a redirect, but it will give the desired result. user1, I agree that Michael Yoon's solution is more elegant, but it would run on EVERY request (even to /MyApp/Other) resulting in needless string compares. You could also add other logic to this action if you needed to send users to another area of the site. But no, it's not a great solution, and Michael Yoon's would be the best for the client.
  • @Laurence Your solution requires two request to server rite ? Thats what I said .
  • @Laurence - I have picked your answer as best answer because it will not do the string compare for every request which URL rewriting will do.
  • I need a solution like this. Let me check and back with my comments, Any way thanks for introducing this one.
  • @MichaelYoon - This seems to be close to what I need. I still dont have it working in my MVC app though. I believe MVC does not have the Application_BeginRequest method since URL rewriting can be done from the RouteConfig.cs file instead
  • Application_BeginRequest may not be there by default in the global.asax, but you can add it manually and it will be called.