Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 视图序列化时链接断开_C#_Asp.net Mvc_Asp.net Mvc 5 - Fatal编程技术网

C# 视图序列化时链接断开

C# 视图序列化时链接断开,c#,asp.net-mvc,asp.net-mvc-5,C#,Asp.net Mvc,Asp.net Mvc 5,我将以下内容作为填充局部视图的视图模型的一部分: public string DownloadLink { get { UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext); switch (this.Type) { case (int)DocumentTypes.WasteNote: {

我将以下内容作为填充局部视图的视图模型的一部分:

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"); }
        }
    }
}
因此认为:

@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>
    }
然后从控制器以Json的形式返回:

        return Json(new { 
            totalCount = (int)Session["DocumentTotalCount"], 
            markup = RenderRazorViewToString("_DocumentListAjax", docs) 
        },JsonRequestBehavior.AllowGet);
但要明确的是,问题发生在JSON序列化之前-这很糟糕,但删除了www前缀:

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

当这样做时,从似乎来自LAN之外的地方(听起来很奇怪),所有的URL都预先设置了www-ie。我正在努力追踪发生这种情况的原因以及如何修复它。

对于您的代码,所描述的行为只能在为不同的请求提供服务时发生。在处理http请求时,必须在另一个视图中呈现部分视图,该http请求的上下文主机设置为
example.com
(例如对的请求)

另一方面,服务包含部分视图的JSON是在处理对
www.example.com
的请求(例如对的请求)后完成的

因为在主视图中渲染局部视图可能已经完成 当通过“正常”url浏览网站时,一切都按计划进行

JSON端点可以通过AJAX查询,检查形成请求的代码——这是罪魁祸首。另一种可能性是,您设置了URL重写以去除
www
前缀,并且由于某些原因,重写规则与JSON服务端点不匹配

更新:

根据附加信息,问题可能与主机配置(IIS和负载平衡器设置)有关。我建议您尝试在web.config文件中添加以下行:

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

...
...
这将强制ASP.NET使用传入请求的主机头
而不是询问服务器名称变量。如果问题的原因确实是配置问题,这应该会有所帮助。

这是如何产生JSON的?@MichałKędrzyński我已经编辑了以下问题:您在ajax调用中使用了什么url,或者ajax调用的url是如何生成的?这是一个完整的url还是一个相对的url?@Kevin它是相对的,根据下面答案中的注释调用了正确的链接您完全正确,部分视图确实是通过
AJAX
调用的,但是链接是相对的,是正确的-调用的url没有www前缀。无论是在工作的内部暂存服务器上,还是在实时环境上,都没有重写规则。因此,基本上在
test.com
的一个页面上,您向
/getPartialViewJson/partialViewName
发出
$.ajax
请求。在发出此请求时,在浏览器中输入的url是
test.com/something
,没有
www
前缀。是这样吗?换句话说,您可以在网络浏览器中看到,
ajax
请求刚好发生在
test.com
上,而没有
www
。我已经更新了答案来说明这一点。你看,我明白了,我很欣赏,虽然这似乎有效,但仍然无效,但似乎有些喜怒无常
<appSettings>
  ...
  <add key="aspnet:UseHostHeaderForRequestUrl" value="true" />
  ...
</appSettings>