Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用$windows.location和ASP.NET MVC编码特殊字符时出现问题_Javascript_C#_Asp.net Mvc_Url_Encoding - Fatal编程技术网

Javascript 使用$windows.location和ASP.NET MVC编码特殊字符时出现问题

Javascript 使用$windows.location和ASP.NET MVC编码特殊字符时出现问题,javascript,c#,asp.net-mvc,url,encoding,Javascript,C#,Asp.net Mvc,Url,Encoding,我有一个javascript代码,其中我对.NETMVC操作执行window.location。id是一个数字e.x.1234,而name是一个带有特殊字符的名称。E.x.“Røed” 在fiddler中,我可以看到请求url变成: mydomain.com/controller/action/?id=1234&name=R%C3%B8ed 当我在ASP.NET MVC控制器中尝试从Request.QueryString获取查询字符串值时,我得到了类似于双编码字符串的内容: public Act

我有一个javascript代码,其中我对.NETMVC操作执行window.location。id是一个数字e.x.1234,而name是一个带有特殊字符的名称。E.x.“Røed”

在fiddler中,我可以看到请求url变成: mydomain.com/controller/action/?id=1234&name=R%C3%B8ed

当我在ASP.NET MVC控制器中尝试从Request.QueryString获取查询字符串值时,我得到了类似于双编码字符串的内容:

public ActionResult MyAction(LandingPage currentPage, string state)
{
    string queryString = Request.QueryString.ToString();
    var cultureName = CultureInfo.CurrentCulture.Name;
查询字符串变为:“id=1234&name=R%u00f8ed”

如您所见,来自请求url的编码看起来与asp.net中的编码不同。为什么?


我需要在我的应用程序(Røed)中进一步使用解码后的名称。我如何才能做到这一点?

在javascript方面尝试一下(以确保正确编码每个部分):

在MVC方面:

public ActionResult Action(string id, string name)
{

}
或者,使用您已经提供的示例:

public ActionResult MyAction(LandingPage currentPage, string state, string id = null, string name = null)
{
    if (id != null && name != null)
    {
    }
}
然后应该正确解释名称。因为您直接使用QueryString,所以它是经过编码的查询字符串


如果确实需要,可以使用
HttpUtility.ParseQueryString(…)
解析查询字符串,这将为您提供
NameValueCollection
,但这不是正确的操作方式。

我们可以查看更多控制器代码吗?我已经更新了控制器代码。我需要简化它,因为它是CMS的一部分。那个控制器看起来和你提供的URL没有多大关系。请看我的答案进行比较。你能更新涉及“LandingPage”classI的问题吗?我不能提供完整的控制器代码,但控制器的目的是根据不同的配置和查询字符串重新重定向到正确的应用程序。因此,querystring可以包括多个参数,而不仅仅是id和name。这就是为什么我们不想在控制器中的每个动作中都有很多参数的原因之一。LandingPage类对于这个问题并不重要。它是EpiServer(CMS)的一个页面类,我在javascript中使用encodedURIComponent时遇到了同样的问题。我无法将参数更改为操作,因为我们的登录页设计,为什么不能添加更多参数?您不必指定请求中的所有参数,您可以执行
string name=null
,这将是可选的。否则,您将需要手动解析查询字符串,这似乎毫无意义。
public ActionResult Action(string id, string name)
{

}
public ActionResult MyAction(LandingPage currentPage, string state, string id = null, string name = null)
{
    if (id != null && name != null)
    {
    }
}