NET中的URL参数编码

NET中的URL参数编码

本文介绍了MVC .NET中的URL参数编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在MVC 4 .NET应用程序中有一个控制器,该控制器从URL接收字符串作为参数.这来自重定向到Route.config中的控制器的aspx页面.

I have a controller in an MVC 4 .NET application that receives a string as a parameter from an URL. This comes from an aspx page redirected to the controller in Route.config.

如果我在客户端中发送此值作为参数:fwdgerhb+bhrth+ftrgbhrt

If I send this value for the parameter in the client: fwdgerhb+bhrth+ftrgbhrt

我在服务器上获得以下值:fwdgerhb bhrth ftrgbhrt

I get the following value at the server: fwdgerhb bhrth ftrgbhrt

服务器将URL参数值解释为编码URL,并将+替换为.但是它尚未经过URL编码.如果特殊字符的其他组合出现在参数值中,则会发生这种情况.

The server is interpreting the URL parameter value as an encoded URL and replaces + by . But it has not been URL encoded. This will occur for other combinations of special chars if they appear in the parameter value.

IIS服务器中是否有一个配置参数来配置服务器,使其不尝试对该值进行URL解码?

Is there a config parameter in IIS Server to configure the server to not try to URL-decode this value?

示例请求:

mypage.aspx?value=cat+dog    (NOT ENCODED)

路由配置

static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRouteLowercase(
        name: "MyRouter",
        url: "mypage.aspx",
        defaults: new { controller = "My", action = "DoLog" }
    );
}

控制器:

public class MyController : Controller
{
    [AllowAnonymous]
    public ActionResult DoLog(string value)
    {
        //Here value has "cat dog"
    }
}

推荐答案

是的,MVC自动对操作参数进行URL解码,但是您仍然可以通过查询字符串访问URL编码的版本.如您在此问题中所看到的: MVC2 ASP.Net URLDecoding是自动生成的吗?

Yes, MVC automatically URL decodes action parameters, but you can still access the URL encoded version through a query string. As you can see in this question: Is MVC2 ASP.Net URLDecoding automatically?

您还可以尝试访问名为 UNENCODED_URL 的服务器变量.可在此处找到有关此方案的更多信息: URL重写模块配置参考

You can also try to access a server variable named UNENCODED_URL. More information about this scenario can be found here: URL Rewrite Module Configuration Reference

这篇关于MVC .NET中的URL参数编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:04