简体   繁体   English

加密字符串在ASP.NET MVC中导致404错误

[英]Encrypted String originates 404 error in ASP.NET MVC

On ASP.NET MVC 5.1 I have an action which receives an encrypted string, for example: 在ASP.NET MVC 5.1上,我有一个接收加密字符串的操作,例如:

Nf7JnWp/QXfA9MNd52RxKpWg= Nf7JnWp / QXfA9MNd52RxKpWg =

But I get a 404 error because of the slash inside this string ... 但由于此字符串中的斜杠,我收到404错误...

I tried to encode the string with HttpUtility.UrlEncode and WebUtility.UrlEncode; 我试图用HttpUtility.UrlEncode和WebUtility.UrlEncode对字符串进行编码;

But I keep having the same problems. 但我一直有同样的问题。 Does anyone knows how to solve this? 有谁知道如何解决这个问题?

Thank You, 谢谢,

Miguel 米格尔

You can build a workaround for this by defining a custom route. 您可以通过定义自定义路由为此构建解决方法。 Now I do not know how you named your controller or your action, so I'll use generic names. 现在我不知道你如何命名你的控制器或你的动作,所以我将使用通用名称。

routes.MapRoute(
    "SpecialControllerName",
    "CustomName/{*id}",
     new { controller = "CustomName", action = "CustomAction", id = UrlParameter.Optional }
);

public ActionResult Name(string id)
{
  //logic goes here

}

So what we did here, is take the action out of the equation. 所以我们在这里所做的就是采取行动。 Now if you call http://yourdomain.com/CustomName/Nf7JnWp/QXfA9MNd52RxKpWg= it will call the Action method CustomName in the Controller CustomNameController . 现在,如果你调用http://yourdomain.com/CustomName/Nf7JnWp/QXfA9MNd52RxKpWg=它会调用Action方法CustomName在控制器CustomNameController

Please note, that the asp.net Framework takes the first route in your route config, which matches its patterns. 请注意,asp.net Framework采用路由配置中的第一条路由,该路由与其模式匹配。 If you have your Defaultroute and place your new custom route below it, it will fail. 如果您有Defaultroute并将新的自定义路线置于下方 ,则会失败。 Placing the Custom route above it, will work 在其上方放置自定义路线将起作用

Similar questions on SO: 关于SO的类似问题:

  1. ActionLink contains slash ('/') and breaks link ActionLink包含斜杠('/')和断开链接

  2. URLs with slash in parameter? 参数中包含斜杠的网址?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM