简体   繁体   中英

Passing a parameter with an ampersand to a controller action method

I have a view that displays a link of vendors, with a link to each vendor detail page

@foreach (var vendor in Model.Vendors)
{
    <li>
        <a href="/Vendors/@(vendor.Name)">@vendor.Name</a>
    </li>
}

One particular vendor is named "ABC Sales & Services" , so the link to their detail page is /Vendors/ABC Sales & Services . When I navigate to that url, I get this error:

A potentially dangerous Request.Path value was detected from the client (&).

Is there a way to get around this?

Add this to web.config 's httpRuntime :

<httpRuntime requestPathInvalidChars="&lt;,&gt;,*,%,:,\,?" />

By default that list includes &amp; , you are simply removing it

您应该真正利用供应商的ID(希望您的数据库上有一个主键-数字/ GUID),而不是名称。

You should UrlEncode the vendor name:

<a href="/Vendors/@Url.Encode(vendor.Name)">@vendor.Name</a>

By the way, this is done on your behalf when you use Url.Action or the Html.Action methods to construct your anchor tags

您可以使用

<a href="@Url.Action("/Vendors/" + @vendor.Name)">@vendor.Name</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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