Reputation: 18869
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?
Upvotes: 3
Views: 2157
Reputation: 3582
You can use
<a href="@Url.Action("/Vendors/" + @vendor.Name)">@vendor.Name</a>
Upvotes: 0
Reputation: 14944
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
Upvotes: 1
Reputation: 4248
You should really utilize the ID of the vendor (hopefully, there is a primary key - either numeric/guid - on your database), not the name.
Upvotes: 1
Reputation: 28272
Add this to web.config
's httpRuntime
:
<httpRuntime requestPathInvalidChars="<,>,*,%,:,\,?" />
By default that list includes &
, you are simply removing it
Upvotes: 4