Asp.Net Routing - Display complete URL

后端 未结 3 723
臣服心动
臣服心动 2020-12-04 02:33

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



        
相关标签:
3条回答
  • 2020-12-04 02:38

    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 }
    );
    
    0 讨论(0)
  • 2020-12-04 02:45

    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.

    0 讨论(0)
  • 2020-12-04 03:00

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

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

    0 讨论(0)
提交回复
热议问题