简体   繁体   中英

ASP.NET Root Url Doesn't Resolve to Default.aspx

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.

在此处输入图片说明

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 }
        );
    }
}

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM