繁体   English   中英

MVC 等效于 jQuery.load

[英]MVC equivalent of jQuery.load

我一直在做一些搜索,并不能完全找到我所追求的。

我正在处理一个 Umbraco 项目,需要在父页面内呈现特定页面的内容。

我试过

@Html.Action("MyAction", "MyController")

但这导致我的页面分配的内容项为空(因为我已经转到操作而不是转到 umbraco 网址),这并不是什么大惊喜。

是否有像Action这样的Html方法接受 url 而不是动作名称和控制器名称?

我在追求这样的事情

@Html.LoadUrl("~/my-controller-section/my-action-page")

我可以自己编写一个扩展来做到这一点,但我真的希望 MVC 中已经内置了一些东西来做到这一点。 我不想重新发明轮子。

我正在使用 ASP.Net MVC 4。

好的,所以在 MVC 中似乎没有办法做到这一点,所以我不情愿地推出了自己的方法。 如果出现这种内置方式,我会将其换掉。

我已经实现了我在问题中提出的 LoadUrl 方法,它的使用方式如下:

@Html.LoadUrl("http://foo/bar")

实现看起来像这样(对不起,它有点长,但我想包含一个完整的工作示例,以便您可以复制和粘贴它)。

您会注意到我在请求上设置了身份验证 cookie,否则它只会返回登录页面而不是请求的页面。 我专门为表单身份验证实现了这一点。 如果您的站点不使用身份验证,则不需要此部分。

public static class MvcExtensions
{
    /// <summary>
    /// Invokes a request to the specified url and returns the result as an HTML string.
    /// </summary>
    /// <param name="thisHtml">The HTML helper instance that this method extends.</param>
    /// <param name="url">The url to invoke the request on.</param>
    /// <returns>The url contents as an HTML string.</returns>
    public static MvcHtmlString LoadUrl(this HtmlHelper thisHtml, string url)
    {
        //get the url as an absolute url
        UrlHelper helper = GetUrlHelper();
        url = helper.Absolute(url);

        var request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
        //set the auth cookie so we don't just get the login page
        request.SetAuthenticationCookie();
        //get the response
        string responseString = request.GetResponseString();
        //return it as an html string
        return new MvcHtmlString(responseString);
    }

    private static UrlHelper GetUrlHelper()
    {
        return new UrlHelper(HttpContext.Current.Request.RequestContext);
    }

    /// <summary>
    /// Gets an absolute version of the specified url relative to the current requests url root.
    /// </summary>
    /// <param name="thisHelper">The Url helper instance that this method extends.</param>
    /// <param name="url">The url to get an absolute version of.</param>
    /// <returns>An absolute version of the specified url relative to the current requests url root.</returns>
    public static string Absolute(this UrlHelper thisHelper, string url)
    {
        //only make the url absolute if it isn't already
        if (!url.Contains("://"))
        {
            return string.Format("http://{0}{1}", thisHelper.RequestContext.HttpContext.Request.Url.Authority, thisHelper.Content(url));
        }
        else
        {
            return url;
        }
    }

    /// <summary>
    /// Sets the authentication cookie of the specified request.
    /// </summary>
    /// <param name="request">The request to set the cookie for.</param>
    /// <param name="authenticationCookie">(Optional) The cookie to add to the request, if not specified defaults 
    /// to the cookie of the user currently logged into the site.</param>
    public static void SetAuthenticationCookie(this HttpWebRequest request, Cookie authenticationCookie = null)
    {
        if (authenticationCookie == null)
        {
            //add the current authentication cookie to the request
            var cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];

            authenticationCookie = new System.Net.Cookie
            (
                FormsAuthentication.FormsCookieName,
                cookie.Value,
                cookie.Path,
                FormsAuthentication.CookieDomain
            );
        }

        request.CookieContainer = new System.Net.CookieContainer();
        request.CookieContainer.Add(authenticationCookie);
    }

    /// <summary>
    /// Gets the response string from the specified request.
    /// </summary>
    /// <param name="request">The request to get the response string from.</param>
    /// <returns>The response string from the specified request.</returns>
    public static string GetResponseString(this System.Net.WebRequest request)
    {
        System.Net.WebResponse response = null;

        try
        {
            response = request.GetResponse();
        }
        catch (System.Net.WebException webException)
        {
            response = webException.Response;
            throw;
        }

        return GetResponseString(response);
    }

    /// <summary>
    /// Gets the response string from the specified response.
    /// </summary>
    /// <param name="response">The response to get the response string from.</param>
    /// <returns>The response string from the specified response.</returns>
    public static string GetResponseString(this System.Net.WebResponse response)
    {
        using (var responseTextStream = new System.IO.StreamReader(response.GetResponseStream()))
        {
            return responseTextStream.ReadToEnd();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM