ASP.NET MVC 路由:如何省略“索引"?从一个网址 [英] ASP.NET MVC Routes: How do I omit "index" from a URL

查看:28
本文介绍了ASP.NET MVC 路由:如何省略“索引"?从一个网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为StuffController"的控制器,带有无参数的索引操作.我希望从 mysite.com/stuff

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

我的控制器定义为

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

}

但是当我尝试浏览到 mysite.com/stuff 时出现错误

But when I try to browse to mysite.com/stuff I get an error

HTTP 错误 403.14 - 禁止

HTTP Error 403.14 - Forbidden

Web 服务器配置为不列出此目录的内容.

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

URL mysite.com/stuff/index 工作正常.我做错了什么?

The URL mysite.com/stuff/index works fine. What I am doing wrong?

推荐答案

HTTP 错误 403.14 - 禁止 Web 服务器配置为不列出此目录的内容.

HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.

该错误表明您的项目中有一个名为 /Stuff 的虚拟目录(可能是物理目录).默认情况下,IIS 将首先到达该目录并寻找一个默认页面(例如 /index.html),如果不存在默认页面,则将尝试列出该目录的内容(这需要一个配置设置).

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).

这一切都发生在 IIS 将调用传递到 .NET 路由之前,因此具有名为 /Stuff 的目录会导致您的应用程序无法正常运行.您需要删除名为 /Stuff 的目录或为您的路线使用不同的名称.

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 路由:如何省略“索引"?从一个网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆