How can I accept an email address as a route value?

How can I get this simple route:

http://domain.com/Calendar/Unsubscribe/my@email.com

I have a route that looks like this:

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

and my action:

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

Without any parameters, for example, http://domain.com/Calendar/Unsubscribe/is operating normally, but soon I add an e-mail, I get a page 404(

Is there any trick I have to do?

thank

+3
source share
6 answers

Try adding http://domain.com/Calendar/Unsubscribe/ my@email.com /trailing slash to the URL without changing the routing rules.

trailing slash 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, .

+4

, .

routes.MapRoute(
"Unsubscribe", 
"Calendar/Unsubscribe/{subscriber}", 
   new { 
   controller = "Calendar", 
   action = "Unsubscribe"
   }
    new { subscriber =  @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" }
);
+1

homecontlor

_http://: 64718/home/index/a.b@email.com

ASP.NET MVC! a.b@email.com

public ActionResult Index(string id)    
{    
    ViewModel.Message = "Welcome to ASP.NET MVC!   " + id;    
    return View();    
}

defaultroute-MVC.

- , Unsubscribe,

+1

subscriber.

0

@ URL-:

http://en.wikipedia.org/wiki/Percent-encoding

; , URL- :

http://domain.com/Calendar/Unsubscribe/my%40email.com
0

rote! Asp.Net Mvc4 - , . Unsubscribe. , .

!

0

Source: https://habr.com/ru/post/1777518/


All Articles