简体   繁体   中英

One ASP.NET MVC route not working

I have a problem with routing, RouteConfig.cs contains these routes:

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

routes.MapRoute(
    "TermsOfService",
    "termsofservice",
    new { controller = "Home", action = "TermsOfService" }
);

routes.MapRoute(
    "PrivacyPolicy",
    "privacypolicy",
    new { controller = "Home", action = "PrivacyPolicy" }
);

routes.MapRoute(
    "Contact",
    "contact",
    new { controller = "Home", action = "Contact" }
);

routes.MapRoute(
    "Support",
    "support",
    new { controller = "Home", action = "Support" }
);

routes.MapRoute(
    "ReadOurStory",
    "readourstory",
    new { controller = "Home", action = "ReadOurStory" }
);

routes.MapRoute(
    name: "BlogItem",
    url: "blog/{name}",
    defaults: new { controller = "Home", action = "BlogItem" }
);

routes.MapRoute(
    name: "ProductDetail",
    url: "products/{name}",
    defaults: new { controller = "Home", action = "Product" }
);

routes.MapRoute(
    name: "Products",
    url: "products",
    defaults: new { controller = "Home", action = "Products" }
);

routes.MapRoute(
    name: "Tutorials",
    url: "tutorials",
    defaults: new { controller = "Home", action = "Tutorials" }
);

routes.MapRoute(
    name: "Blog",
    url: "blog",
    defaults: new { controller = "Home", action = "Blog" }
);

routes.MapRoute(
    name: "TutorialDetail",
    url: "tutorials/{name}",
    defaults: new { controller = "Home", action = "Tutorial" }
);

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

and these two actions in HomeController :

public ActionResult Products()
{
    var model = LoadItemsModel(ItemType.Product);
    return View(model);
}

public ActionResult Tutorials()
{
    var model = LoadItemsModel(ItemType.Tutorial);
    return View(model);
}

Now the link for products is working:

http://localhost:61296/products/

but the link for tutorials:

http://localhost:61296/tutorials/

returns error

HTTP Error 403.14 - Forbidden

The Web server is configured to not list the contents of this directory

I tried to change the route just for a test to tutorials2 and the action to Tutorials2 and then this modified link is working. I do not know why the route tutorials does not work.

Based on the error, my guess would be that you have a physical directory in your project folder called "tutorials". Any physical files/directories will always overrule any routes. Then, since directory listing is disabled by default, you get that 403 error. Remove or rename the physical directory and you should be fine.

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