c# - 如何避免在c#MVC中与号(&)被转换为&

chi*_*hii 2 c# asp.net-mvc

我使用 MVC 模型制作图像 url 并显示它。

我使图像网址是这样的:

        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());
Run Code Online (Sandbox Code Playgroud)

我有一个名为 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");

    }
Run Code Online (Sandbox Code Playgroud)

在视图中,我是这样写的:

<a><img class="shadow" src="<%:Model.ImageUrls[1] %>"/></a>
Run Code Online (Sandbox Code Playgroud)

这实际上奏效了。但有时会出现此错误:

System.NullReferenceException: Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)

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

[Request.RawUrl]/Work/LookPage?pbid=PBER-2972311310171610590&amp;pg=8&amp;isStage=true
Run Code Online (Sandbox Code Playgroud)

因为 url 中的 & 正在转换为 & 所以路由不起作用。我怎样才能避免 & 符号(&)被转换为 & ?

我也不知道为什么有时 url 是好的(&)有时不是(正在转换)

小智 5

尝试这个。它将编码的字符串转换为解码的 html 标记。即删除&amp;并替换为'&'

Html.Raw(Model.ImageUrls[1]);
Run Code Online (Sandbox Code Playgroud)