asp.net-mvc - asp.net mvc 路由 : how to use default action but non-default parameter?

标签 asp.net-mvc routing url-routing asp.net-mvc-routing

我正在重写这个问题,因为到目前为止的答案告诉我,我对它的定义还不够好。我将在下面留下原始问题以供引用。

设置路由时,您可以为不同的 url/路由部分指定默认值。让我们考虑 VS 向导生成的示例:

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "DefaultPage", action = "Index", id = UrlParameter.Optional } // Parameter defaults

在此示例中,如果未指定 Controller ,则将使用 DefaultPageController,如果未指定操作,则将使用“索引”操作。
URL 通常看起来像:http://mysite/MyController/MyAction .

如果 Url 中没有这样的操作:http://mysite/MyController然后将使用索引操作。

现在假设我的 Controller Index 和 AnotherAction 中有两个 Action 。对应的url是http://mysite/MyControllerhttp://mysite/MyController/AnotherAction分别。我的“索引”操作接受一个参数 id。因此,如果我需要将参数传递给我的索引操作,我可以这样做:http://mysite/MyController/Index/123 .请注意,这与 URL http://mysite/MyController 不同。 ,我必须明确指定索引操作。我想做的是能够通过http://mysite/MyController/123而不是 http://mysite/MyController/Index/123 .我在这个 URL 中不需要“索引”,我希望 mvc 引擎能够识别,当我请求 http://mysite/MyController/123 时,那 123 不是一个 Action (因为我没有用这个名字定义一个 Action ),而是我的默认 Action “索引”的一个参数。我如何设置路由来实现这一点?

以下是问题的原始措辞。

我有一个 Controller ,其中定义了两种方法
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(SomeFormData data)
    {
        return View();

    }

这使我可以像 http://website/Page 一样处理 Url当用户导航到这个 url (GET) 和他们随后发回表单 (POST) 时。

现在,当我处理回帖时,在某些情况下,我想将浏览器重定向到这个 url:
http://website/Page/123

其中 123 是某个整数,我需要一种方法来在我的 Controller 中处理此 url。

如何设置路由,这样可以吗?目前我有向导生成的“默认”路由,如下所示:
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "DefaultPage", action = "Index", id = UrlParameter.Optional } // Parameter defaults

我尝试添加另一个 Controller 方法,如下所示:
public ActionResult Index(int id)
{
    return View();
}

但这不起作用,因为引发了模棱两可的 Action 异常:

The current request for action 'Index' on controller type 'PageController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult Index() on type PageController System.Web.Mvc.ActionResult Index(Int32) on type PageController



我必须补充一点,我在这个 Controller 中还有其他操作。 This如果我不这样做就可以了。

最佳答案

哦,我现在明白了。我认为默认路由是不可能的,您需要映射自定义路由。

    // /MyController/AnotherAction
    routes.MapRoute(
        null,
        "MyController/AnotherAction",
        new { controller = "DefaultPage", action = "AnotherAction" }
    );

    // /MyController
    // /MyController/id
    routes.MapRoute(
        null,
        "MyController/{id}",
        new { controller = "DefaultPage", action = "Index", id = UrlParameter.Optional }
    );

附言。像/MyController/id 这样的默认路由必须最后映射。

关于asp.net-mvc - asp.net mvc 路由 : how to use default action but non-default parameter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5591569/

相关文章:

javascript - AngularJS - 从 .NET Controller 生成 View 只执行一次

rest - Nginx 路由到多个 RESTful 端点

symfony - 路由 "admin/login"不存在错误 Symfony2 + SonataAdminBundle

javascript - EmberJS : How to provide a specific URL for a model in ember-data RESTAdapter?

Django URLConf 包含子应用程序不工作

ASP.NET 路由导致页面资源无法加载

c# - 如何在MVC中等待await函数时返回 View

asp.net - 是否有任何ASP.Net MVC引用应用程序

c# - 从异常详情中获取异常发生的类名和方法名

asp.net-mvc - 从 ASP.NET MVC 中的相同路由 URL 路径提供图像