1

I have an asp.net web site that won't seem to resolve to Default.aspx when initially loading. When I debug on my local machine it loads Default no problem. Unless I try to navigate to "localhost:#####/". Then it gives a 404 error. When I deploy it to a staging server, give it a virtual path "mywebapp", and load it from "mydomain.com/mywebapp" it gives a 404 as well. I have set Default.aspx to the top of the list for Default Document in IIS. If I navigate to "mydomain.com/mywebapp/default" the site loads just fine. Any suggestions? I would paste code but it is a large website and I quite honestly am not sure what I'm looking for anymore.

enter image description here

EDIT: In my site I am also using DataTables for display and edit of data. In the ajax calls I was previously able to call the controller by using urls such as:

api/MyController/idvalue

but since uncovering this I had to go back and preface the urls to get them to work:

mywebapp/api/MyController/idvalue

Controller:

public class MyController : ApiController
{
    [Route("api/MyContoller/{idvalue}")]
    [HttpGet]
    [HttpPost]
    public IHttpActionResult MyControllerMethod(intidvalue)
    {
    }
}

WebApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

RouteConfig:

 public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        //routes.MapRoute(
        //        name: "Default",
        //        url: "{controller}/{action}/{id}",
        //        defaults: new { action = "Index", id = UrlParameter.Optional }
        //    );
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}
mac
  • 485
  • 1
  • 6
  • 29
  • Is this a MVC or regular webforms site? The reason I ask is to know if you have Routers setup. Also have you tried adding the default page to the web.config? https://stackoverflow.com/questions/1913058/set-default-page-in-asp-net. Also have you tried mydomain.com/mywebapp/ (with the slash at the end)? – Jonathas Sucupira Nov 10 '17 at 23:31
  • It is and it isn't if that makes sense. It's webforms mostly but some aspects of MVC are used. I have tried the slash at the end as well. The solution in that article did not solve it. I have edited my question to note something else interesting – mac Nov 13 '17 at 13:35

1 Answers1

1

Found the solution. In my RouteConfig.cs if I comment out the line:

controller = "Home",

it works just fine.

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

However, I'm not completely sure why this is. So if anyone is able to explain it to me that would be great!

mac
  • 485
  • 1
  • 6
  • 29
  • This was the reason why I asked if you had a MVC router. Previously the default page was been set by our MVC router. Your "fix" above all it did was to "break" the default route. Since you no longer have a default controller specified, IIS will be the one telling which page is your new default. – Jonathas Sucupira Nov 13 '17 at 22:05
  • Thank you for the explanation. – mac Nov 14 '17 at 12:33