Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I accept email address as a route value?

How can I have this simple Route:

http://domain.com/Calendar/Unsubscribe/[email protected]

I have a route that looks like:

routes.MapRoute(
    "Unsubscribe", 
    "Calendar/Unsubscribe/{subscriber}", 
    new { 
       controller = "Calendar", 
       action = "Unsubscribe", 
       subscriber = "" }
);

and my action is:

public ActionResult Unsubscribe(string subscriber)
{
    ...
}

Without any parameters, like http://domain.com/Calendar/Unsubscribe/ works fine, but soon I add the email, I get a 404 page :(

Is there any trick I have to do?

Thank you

like image 369
balexandre Avatar asked Dec 19 '25 05:12

balexandre


1 Answers

Try adding a trailing slash to the url http://domain.com/Calendar/Unsubscribe/[email protected]/ without changing the routing rules.

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>

You could also write a better regular expression than the one I provided for the sake of readability.

You could also try to reorder your route params like /Calendar/[email protected]/Unsubscribe so that the e-mail is not the last param.

like image 135
Jani Hyytiäinen Avatar answered Dec 21 '25 02:12

Jani Hyytiäinen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!