chii
chii

Reputation: 2209

how to avoid Ampersand(&) being converted to &amp in c# MVC

I use MVC model to make the image url and to display it .

I make the image url like this:

        string baseURL = string.Format("https://abc/Work/LookPage?pbid={0}", model.ID);

        var builder = new UriBuilder(baseURL);
        var query = HttpUtility.ParseQueryString(builder.Query);
        query["pg"] = "1";
        query["isStage"] = "false";
        builder.Query = query.ToString();
        model.ImageUrls.Add(1, builder.ToString());

and I have a controller named WorkController

    [OutputCache(Duration = 600, VaryByParam = "*", Location = OutputCacheLocation.Client)]
    public virtual ActionResult LookPage(string pbid, int pg, bool? isbody, bool? isStage)
    {
        var ms = this.CreateLookPageImage(pbid, pg, isbody, isStage, false);

        if (ms == null)
            return HttpNotFound();

        return new FileStreamResult(ms, "image/jpeg");

    }

and in the view , i write like this:

<a><img class="shadow" src="<%:Model.ImageUrls[1] %>"/></a>

This worked actually. but sometimes this error comes:

System.NullReferenceException: Object reference not set to an instance of an object.

at System.Web.HttpCookieCollection.Add(HttpCookie cookie) at CWorks.Web.Filter.SiteSelectorAttribute.OnActionExecuting(ActionExecutingContext filterContext)

[Request.RawUrl]/Work/LookPage?pbid=PBER-2972311310171610590&amp;pg=8&amp;isStage=true

because & in the url is being converted to &amp; so the routing doesnot work. how can i avoid the Ampersand(&) being converted to &amp??

I also donot know why sometimes the url is ok(&) and sometime is not(being converted)

Upvotes: 3

Views: 5479

Answers (2)

Namdev Karande
Namdev Karande

Reputation: 81

Try this. It converts encoded string into decoded html mark up. i.e removes &amp; and replaces with '&'

Html.Raw(Model.ImageUrls[1]);

Upvotes: 7

serhiyb
serhiyb

Reputation: 4833

https://msdn.microsoft.com/en-us/library/gg480740(v=vs.118).aspx

<a><img class="shadow" src="<%= Html.Raw(Model.ImageUrls[1]) %>"/></a>

Upvotes: 2

Related Questions