5

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?

3 Answers 3

2

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 }
);
Sign up to request clarification or add additional context in comments.

5 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.
2

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.

3 Comments

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.
1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.