我怎么能有这个简单的路线: http://domain.com/Calendar/Unsubscribe/my@email.com 我的路线看起来像: routes.MapRoute( "Unsubscribe", "Calendar/Unsubscribe/{subscriber}", new { controller = "Calendar", action = "Unsubscrib
http://domain.com/Calendar/Unsubscribe/my@email.com
我的路线看起来像:
routes.MapRoute( "Unsubscribe", "Calendar/Unsubscribe/{subscriber}", new { controller = "Calendar", action = "Unsubscribe", subscriber = "" } );
我的行动是:
public ActionResult Unsubscribe(string subscriber) { ... }
没有任何参数,如http://domain.com/Calendar/Unsubscribe/工作正常,但很快我添加电子邮件,我得到一个404页面:(
我有什么秘诀吗?
谢谢
尝试在不更改路由规则的情况下向网址 http://domain.com/Calendar/Unsubscribe/my@email.com/添加尾部斜杠.如果您仍想避免添加尾部斜杠并且可以使用URL Rewrite,则可以在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>
为了便于阅读,您还可以编写一个比我提供的更好的正则表达式.
您也可以尝试重新排序路线参数,例如/Calendar/my@email.com/Unsubscribe,以便电子邮件不是最后一个参数.