我怎么能接受的电子邮件地址作为路由值? [英] How can I accept email address as a route value?
问题描述
我怎么能有这种简单的路线:
How can I have this simple Route:
http://domain.com/Calendar/Unsubscribe/my@email.com
我有,看起来像一个路线:
I have a route that looks like:
routes.MapRoute(
"Unsubscribe",
"Calendar/Unsubscribe/{subscriber}",
new {
controller = "Calendar",
action = "Unsubscribe",
subscriber = "" }
);
和我的动作是:
public ActionResult Unsubscribe(string subscriber)
{
...
}
在没有任何参数,如 http://domain.com/Calendar/Unsubscribe/
工作正常,但很快我添加的电子邮件,我收到了 404
页:(
Without any parameters, like http://domain.com/Calendar/Unsubscribe/
works fine, but soon I add the email, I get a 404
page :(
有没有招我有什么关系?
感谢您
推荐答案
尝试添加斜线
来的URL的 http://domain.com/Calendar/Unsubscribe/my@email.com/ 不改变路由规则。
Try adding a trailing slash
to the url http://domain.com/Calendar/Unsubscribe/my@email.com/ without changing the routing rules.
如果你仍然想避免添加斜线
和你有URL重写可用,您可以添加一个重写规则到web.config中
If you still want to avoid adding the trailing slash
and you have URL Rewrite available, you could add a rewrite rule into the web.config
<system.webServer>
<rewrite>
<rules>
<rule name="Fix Unsubscribe emails route" stopProcessing="true">
<match url="^(Calendar/Unsubscribe/.*@.*)$" />
<action type="Rewrite" url="{R:1}/" />
</rule>
</rules>
</rewrite>
</system.webServer>
您也可以写出更好的常规前pression比我提供可读性的缘故之一。
You could also write a better regular expression than the one I provided for the sake of readability.
您也可以尝试重新安排你的路线PARAMS像/Calendar/my@email.com/Unsubscribe使电子邮件是不是最后的参数。
You could also try to reorder your route params like /Calendar/my@email.com/Unsubscribe so that the e-mail is not the last param.
这篇关于我怎么能接受的电子邮件地址作为路由值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!