简体   繁体   English

MVC ASP.NET查询字符串不正确

[英]MVC ASP.NET querystring incorrect

I have a problem with my querystring, i call an action in my controller with javascript like this 我的查询字符串有问题,我在控制器中使用javascript这样调用了一个操作

if ((event >= 65) && (event <= 90) || (event >= 48) && (event <= 57)) {
    var focussed = document.activeElement.id;
    window.setTimeout(function () {
        alert(in2.value);
        location.href = "/CarSaldi/ListaArt?in1=" + in1.value + "&in2=" + in2.value + "&focus=" + focussed;
    }, 1000);
}

in2 is a input text and might have a "+" inside it (for example "milk+chocolate"), when i call the action in my controller 当我在控制器中调用动作时,in2是输入文本,并且可能在其中包含“ +”(例如“ milk + chocolate”)

public ActionResult ListaArt(string in1, string in2, string cod, string focus)
{
    [...]
}

the string in2 shows my "milk chocolate", i expected milk+chocolate...i need to have also the "+" inside. 字符串in2显示我的“牛奶巧克力”,我希望牛奶+巧克力...我还需要里面有“ +”。

You should use java script function encodeURIComponent to encode url. 您应该使用Java脚本函数encodeURIComponent对URL进行编码。

location.href = "/CarSaldi/ListaArt?in1=" + encodeURIComponent(in1.value) 
 + "&in2=" + encodeURIComponent(in2.value) + "&focus=" + encodeURIComponent(focussed);

You need to change you code as follows 您需要按以下方式更改代码

if ((event >= 65) && (event <= 90) || (event >= 48) && (event <= 57)) {
var focussed = document.activeElement.id;
window.setTimeout(function () {
    alert(in2.value);
    location.href = "/CarSaldi/ListaArt?in1=" + encodeURIComponent(in1.value) 
 + "&in2=" + encodeURIComponent(in2.value) + "&focus=" + encodeURIComponent(focussed);
  }, 1000);
}

Use encodeURIComponent(yourParameter) . 使用encodeURIComponent(yourParameter)

In this case special characters are not lost 在这种情况下,特殊字符不会丢失

Plus sign should be URL encoded in your case: 在您的情况下,加号应使用URL编码:

 +   =  %2B

So instead of using ' + ' in the URL use ' %2B ' 因此,不要在URL中使用“ + ”,而应使用“ %2B

See the full table of URL encoded characters: http://www.degraeve.com/reference/urlencoding.php 请参阅URL编码字符的完整表: http : //www.degraeve.com/reference/urlencoding.php

Encode your string using the javascript function encodeURIComponent : 使用javascript函数 encodeURIComponent编码您的字符串:

you can use: 您可以使用:

 encodeURIComponent(in1.value)

Never, never, never construct your query string in asp.net mvc using javascript and string conatenation. 从不,从不,从不使用javascript和字符串连接在asp.net mvc中构造查询字符串。 Always let this to Url.Action . 始终将其发送给Url.Action The main reason is that what is left on querystring and what is inside the pathinfo depends on how your routes are defined. 主要原因是查询字符串上剩下的内容和pathinfo中的内容取决于路由的定义方式。 If you want to give yourself the chance to change your routes easily in the future, use always Url.Action : 如果您希望将来有机会轻松更改路线,请始终使用Url.Action

var uri = "@Url.Action("ListaArt", CarSaldi", new {in1="xxx", in2="yyy", focus="zzz"})".replace("&amp;","&");
// Replace the "placeholders" values to the real ones
uri = uri.replace("xxx", in1.value);
uri = uri.replace("yyy", in2.value);
uri = uri.replace("yyy", focussed);
location.href = uri;

And regarding the plus sign you need to use encodeURIComponent method in Javascript: 对于加号,您需要在Javascript中使用encodeURIComponent方法:

uri = uri.replace("yyy", encodeURIComponent(in2.value));

Hope this helps! 希望这可以帮助!

PS: The replace("&amp;","&") is needed because Url.Action generates URLs using & instead of & for separating the tokens of the querystring. PS:需要replace("&amp;","&") ,因为Url.Action使用&而不是&生成URL来分隔查询字符串的标记。 This is good for HTML (you can put this in the href of a A tag) but not for the location.href property. 这对HTML很有用(您可以将其放在A标签的href中),而不适合location.href属性。

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

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