ASP.NET MVC路线:怎样省略"指数"从URL [英] ASP.NET MVC Routes: How do I omit "index" from a URL
问题描述
我有一个名为StuffController有一个无参数指标的动作控制器。我想从形式 mysite.com/stuff
我的控制器被定义为
公共类StuffController:BaseController
{
公众的ActionResult指数()
{
//返回的东西名单
}
}
我添加使路由这样定义一个定制路线:
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO); //自定义路线显示指数
routes.MapRoute(
名称:StuffList
网址:东西,
默认:新{控制器=东西,行动=索引}
);
routes.MapRoute(
名称:默认,
网址:{控制器} / {行动} / {ID}
默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
);}
但是当我尝试浏览到 mysite.com/stuff
我得到一个错误
HTTP错误403.14 - 禁止
Web服务器配置为不列出此目录的内容。
该网址 mysite.com/stuff/index
工作正常。我做错了吗?
HTTP错误403.14 - 禁止访问Web服务器配置为不列出此目录的内容
块引用>该错误表明您在您的项目称为
/东西
虚拟目录(可能是一个物理的)。默认情况下,IIS将首先到达这个目录下,寻找一个默认的页面(例如/index.html
),如果没有默认页面是否存在将尝试列表中的内容目录(这需要配置设置)。这一切都发生IIS将调用.NET路由之前,所以有导致您的应用程序不能正常运行名为
/东西的目录。您需要删除
/东西
命名的目录或您的路线使用不同的名称。和如其他人提及的,默认路由,所以没有必要在这种情况下,一个自定义路由覆盖此方案
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO); //传递的URL`/ Stuff`将匹配这条路线,并使其
//寻找一个名为`名为`Index`行动StuffController`控制器。
routes.MapRoute(
名称:默认,
网址:{控制器} / {行动} / {ID}
默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
);
}I have a controller called "StuffController" with a parameterless Index action. I want this action to be called from a URL in the form
mysite.com/stuff
My controller is defined as
public class StuffController : BaseController { public ActionResult Index() { // Return list of Stuff } }
I added a custom route so the routes are defined like this:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // Custom route to show index routes.MapRoute( name: "StuffList", url: "Stuff", defaults: new { controller = "Stuff", action = "Index" } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }
But when I try to browse to
mysite.com/stuff
I get an errorHTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.
The URL
mysite.com/stuff/index
works fine. What I am doing wrong?解决方案HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.
The error indicates that you have a virtual directory (probably a physical one) in your project called
/Stuff
. By default, IIS will first reach this directory and look for a default page (for example/index.html
), and if no default page exists will attempt to list the contents of the directory (which requires a configuration setting).This all happens before IIS passes the call to .NET routing, so having a directory with the name
/Stuff
is causing your application not to function correctly. You need to either delete the directory named/Stuff
or use a different name for your route.And as others have mentioned, the default route covers this scenario so there is no need for a custom route in this case.
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // Passing the URL `/Stuff` will match this route and cause it // to look for a controller named `StuffController` with action named `Index`. routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }
这篇关于ASP.NET MVC路线:怎样省略"指数"从URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!