简体   繁体   English

ASP.NET MVC ActionLink:生成奇怪的URL

[英]ASP.NET MVC ActionLink: Strange URL generated

Why does the following Html.ActionLink call: 为什么以下Html.ActionLink调用:

Html.ActionLink("Approve", "Advance", new { id = Model.ID, step = StepType.Approve })

generate a URL with query parameters rather than a "restful" URL, ie: 生成带有查询参数的URL,而不是“ restful” URL,即:

http://localhost/Website/Case/Advance/1?step=Refer

I only have the default route registered, do I need additional routes that can understand what the "StepType" parameter is? 我只注册了默认路由,是否需要其他可以理解“ StepType”参数是什么的路由?

I've tried adding this route in after the default route: 我尝试在默认路由之后添加此路由:

routes.MapRoute(
    "CaseAdvance",
    "{controller}/{action}/{id}/{step}",
    new {controller = "Case", action = "Advance", id = "", step = StepType.Refer});

but it had no effect. 但没有效果。 Adding the new route registration before the default gave me an error: 在默认设置之前添加新的路由注册会给我一个错误:

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int64' for method 'System.Web.Mvc.ActionResult Advance(Int64, Website.Core.StepType)' in 'Website.Controllers.CaseController'. 参数字典包含“ Website.Controllers.CaseController”中方法“ System.Web.Mvc.ActionResult Advance(Int64,Website.Core.StepType)”的非空类型“ System.Int64”的参数“ id”的空条目”。 To make a parameter optional its type should be either a reference type or a Nullable type. 要使参数成为可选参数,其类型应为引用类型或Nullable类型。

是的,如果没有诸如“ {controller} / {action} / {id} / {step}”之类的路由,则ActionLink方法将简单地将“ step”作为querystring参数传递。

Custom route catching too much at the moment 自定义路线目前过多

Your exception only tells you that your custom route is catching something that you didn't intend it to catch. 您的例外仅告诉您您的自定义路线正在捕获某些您不希望捕获的东西。 So if there would be a request to your application root URL: 因此,如果对您的应用程序根URL有请求:

http://localhost/Website HTTP://本地主机/网站

your custom route would catch it. 您的自定义路线会抓住它。 And set it's defaults. 并将其设置为默认值。 And call your CaseController.Advance() action. 并调用您的CaseController.Advance()操作。 And of course throw an exception, because id is not defined. 当然会抛出异常,因为未定义id

Keep your custom route in front of default one 将您的自定义路线放在默认路线的前面

But you will have to change your custom route or add route constraints to it so it will actually catch only those requests that it's meant to catch. 但是您必须更改自定义路由或向其添加路由约束,以便它实际上仅捕获要捕获的那些请求。

But which change should you do? 但是,您应该做什么更改? If there's only going to be a single controller that needs it than change it to: 如果只需要一个控制器,则将其更改为:

routes.MapRoute(
    "CaseAdvance",
    "Case/{action}/{id}/{step}",
    new { controller = "Case", action = "Advance", id = "", step = StepType.Refer});

If there are other controllers a well, you can keep it as it was, just add constraints: 如果还有其他控制器,则可以保持原样,只需添加约束即可:

routes.MapRoute(
    "CaseAdvance",
    "{controller}/{action}/{id}/{step}",
    new { controller = "Case", action = "Advance", id = "", step = StepType.Refer},
    new { controller = "Case|Other" });

If there can be any controller, you can make a requirement for your id to be numeric: 如果可以有任何控制器,则可以要求ID为数字:

routes.MapRoute(
    "CaseAdvance",
    "{controller}/{action}/{id}/{step}",
    new { controller = "Case", action = "Advance", id = "", step = StepType.Refer},
    new { id = @"\d+" })

In this case this route will only catch those requests that actually have an id defined. 在这种情况下,此路由将仅捕获那些实际定义了ID的请求。 As a number of course. 当然有一些。

You will know which one suits you best. 您会知道哪一个最适合您。

In your route you have specified a parameter of stepType but your passing a parameter called step. 在您的路线中,您指定了stepType参数,但您传递了称为step的参数。

Your actionlink parameters names must match the route parameter names or you will get exactly what your seeing. 您的actionlink参数名称必须与路由参数名称匹配,否则您将获得所见即所得的结果。

EDIT: Ok, you changed your code while I was typing this answer!! 编辑:好的,您在我输入此答案的同时更改了代码!

Try using RouteLink and see if that works for you. 尝试使用RouteLink ,看看是否适合您。

Html.RouteLink("Approve", "CaseAdvance", new { controller = "Case", action = "Advance", id = Model.ID, step = StepType.Approve })

If calling RouteLink produces a valid link it will at least mean your route is setup correctly. 如果调用RouteLink产生有效链接,则至少意味着您的路由已正确设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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