允许MVC航线空参数 [英] Allow empty parameter in MVC route

查看:72
本文介绍了允许MVC航线空参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该由一个自动化的过程只会被调用的路线:

  routes.MapRoute(
  自动文件处理,
  过程/ {变化} / {文件} / {类型},
  新{控制器=C,行动=过程}
);

这里既有文件键入是可选的参数。理想情况下,我想能够调用

  /工艺/创建/文件名/文本(带文件和类型)
/工艺/ Directory上市//文本(带型专用)
/工艺/创建/文件名/(与文件只)

你会如何acheive在中间的那个可选参数?随着例子路线我发现,即使我添加文件=,键入=来的路线,我得到:


  

HTTP错误400 - 错误的请求



解决方案

您不能在路线中间的可选参数。由于显而易见的原因,只有最后参数可以是可选的,或者路由引擎无法在不同的可能的情况间的歧义。

I've got a route that should only ever called by an automated process:

routes.MapRoute(
  "Automated file processing",
  "Process/{change}/{file}/{type}",
  new { controller = "C", action = "Process" }
);

Where both the file and type are optional parameters. Ideally, I'd like to be able to call

/Process/Created/Filename/Text    (with file and type)
/Process/DirectoryListing//Text   (with type only)
/Process/Created/Filename/        (with file only)

How would you acheive that optional parameter in the middle? With the example route I showed, even if I add file = "", type = "" to the route, I get:

HTTP Error 400 - Bad Request.

解决方案

You cannot have optional parameters in the middle of route. For obvious reasons only the last parameter can be optional or the routing engine cannot disambiguate between the different possible cases.

这篇关于允许MVC航线空参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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