python 分隔符为逗号或顿号_ASP.NET MVC - 空的参数的顿号,作为分隔符

How should I define route in my global.asax to be able use nullable parameters and coma as separator?

I'm trying to implement routing rule for my search users page like

"{Controller}/{Action},{name},{page},{status}"

Full entry from the Global.asax:

routes.MapRoute(

"Search",

"{controller}/{action},{name},{page},{status}",

new { controller = "User", action = "Find",

name = UrlParameter.Optional,

page = UrlParameter.Optional,

status = UrlParameter.Optional }

);

Routine defined like above works fine when I'm entering all parameters, but when some parameters are equal to null routing fails (for example "user/find,,,")

According to Clicktricity comment bellow - the singature of action method that handes the request:

public ActionResult Find(string userName, int? page, int? status)

{

// [...] some actions to handle the request

}

On the beginning I was testing the route by VS debugger, now I'm using route debugger described on Phil's Haack blog. The tool confirm - that routing with null values is not properly handled (or I'm doing something wrong ;) )

解决方案

As far as I know .Net routing doesn't let you do multiple nullable parameters like that. Multiple parameters will only work if they are missing working backwards from the end and with the separator also missing so you'd get matches on

user/find,bob,2,live

user/find,bob,2

user/find,bob

user/find

It'd be a lot easier to use querystrings for what you're trying to do.

Edit based on comment:

If this is necessary then you could try doing it this way (though it's not a nice approach)

Change your path to match

{Controller}/{Action},{*parameters}

Make sure to put a constraint on the action and controller so this is limited to as few as possible.

Rename each action that would take your full list to something else, adding a standard prefix to each one would be the cleanest way, and add the [NonAction] attribute. Add a new method with the original name that takes a string, this string is a comma separated string of your variables. In this method split the string and return the original action passing in the values from the split.

So you go from:

public ActionResult Find(string name, int page, string status){

//Do stuff here

return View(result);

}

To

public ActionResult Find(string parameters){

string name;

int? page;

string status;

//split parameters and parse into variables

return FindAction(name, page, status);

}

[NonAction]

public ActionResult FindAction(string parameters){

//Do what you did in your previous Find action

return View(results);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值