Search code examples
asp.net-mvciis-8.5

ASP.NET MVC on IIS - URL/routing to start page after publish not correct


I am developing an ASP.NET MVC page. Debugging on my development machine with IIS Express works just fine, start page is localhost:xxxx/Home/Index.

After publishing on "real" IIS (different machine, version 8.5), the start page does not have the correct URL. The page of Home/Index is displayed but shows simply as http://[servername]:[port]. Route /Home/Index does not seem to be applied.

Problem: the page is using AJAX for an autocomplete field. Of course the action is then "not found", because in the AJAX call, the URL is wrong (controller name missing).

I put an action link on top that results in /Home/Index. I click it, /Home/Index is properly used and displayed, autocomplete works as expected.

Is this some mistake I made in IIS config or in my ASP.NET MVC application?

My RouteConfig:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

Global.asax:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

Solution

  • Since everything else failed, I decided: why not do it in a simple way. Did this now:

    $(document).ready(function () {
    if (window.location.href.indexOf("Home")==-1){
        window.location.href = window.location.href+"Home/Index";
        }
    ...
    

    All other suggested attempts had no effect.