Link breaking when viewing Serialized

I have the following part of a view model that fills in a partial view:

public string DownloadLink { get { UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext); switch (this.Type) { case (int)DocumentTypes.WasteNote: { return url.Action("PaperWork", "Jobs", new { JobId = this.DOCJobId }, HttpContext.Current.Request.IsSecureConnection ? "https" : "http" ); } case (int)DocumentTypes.Contract: case (int)DocumentTypes.DestructionCert: case (int)DocumentTypes.Quote: default: { return url.Action("Download", "Documents", new { DocId = this.DocumentLinkId }, HttpContext.Current.Request.IsSecureConnection ? "https" : "http"); } } } } 

Called in a view this way:

 @model IEnumerable<Document> @using CustomerDashboard.ViewModels; @using CustomerDashboard.Utilities; @{ Layout = null; } @foreach (Document doc in Model) { <li> <a href="@doc.DownloadLink" class="documentDownload"> <div class="col-sm-12 col-md-5 li-row li-row-icon"> <div class="pull-left"> <img height="40" src="/Content/img/file-icons/@doc.Icon" /> </div> ... </a> </li> } 

So far this is being visualized directly in a different view. The generated link is fine, however I serialize the partial and return as JSON:

 public string RenderRazorViewToString(string viewName, object model) { //https://gist.github.com/timiles/2828976 ViewData.Model = model; using (var sw = new StringWriter()) { var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); viewResult.View.Render(viewContext, sw); viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View); return sw.GetStringBuilder().ToString(); } } 

Then it returns as Json from the controller:

  return Json(new { totalCount = (int)Session["DocumentTotalCount"], markup = RenderRazorViewToString("_DocumentListAjax", docs) },JsonRequestBehavior.AllowGet); 

To be clear, the problem is JSON serialization - this is nasty, but removes the www prefix:

  return Json(new { totalCount = (int)Session["DocumentTotalCount"], markup = RenderRazorViewToString("_DocumentListAjax", docs).Replace("www.","") },JsonRequestBehavior.AllowGet); 

At the same time, from what seems to be outside the local network (which sounds strange), all URLs have www preended - i.e. https://example.com becomes https://www.example.com . I am trying to find out why this is happening and how to fix it.

+6
source share
1 answer

When using your code, the described behavior can occur only when serving various requests. The provision of a partial view inside another view should result from processing an HTTP request whose context host is set to example.com (for example, the request https://example.com/viewWithAPartialView ).

A partial view JSON service, on the other hand, is executed as a result of processing a request to www.example.com (for example, a request https://www.example.com/getPartialViewJson/partialViewName ).

Since the rendering of the partial view inside its main view is probably done while viewing the website through its โ€œregularโ€ URL, everything goes according to plan.

The JSON endpoint can be requested through AJAX, check the code that forms the request is a criminal. Another possibility is that you have a URL rewrite configured to split the www prefix, and for some reason the rewrite rule does not match the JSON service endpoint.

Update:

Given the additional information, it seems that the problem may be related to the host configuration (IIS settings and load balancing). I can suggest to try adding the following line to your web.config file:

 <appSettings> ... <add key="aspnet:UseHostHeaderForRequestUrl" value="true" /> ... </appSettings> 

This will force ASP.NET to use the host header from incoming requests instead of requesting a server name variable. If the cause of the problem is a configuration problem, this should help.

+1
source

All Articles