【问题标题】:Reverse Proxy Not working for Angular App反向代理不适用于 Angular 应用程序
【发布时间】:2016-08-30 10:07:43
【问题描述】:

您好,我正在尝试在不使用 iFrame 、 Object 和 embed 标签的情况下向我的应用程序打开托管的 Angular 应用程序。下面是我的处理程序代码。 Css 和 js 文件已正确加载,但网站未按预期工作。

**web.config app settings:**

<add key="ProxyMode" value="1"/>
<add key="RemoteWebSite" value="http://localhost/angulartest"/> 

**Handler :**       

public class ReverseProxy : IHttpHandler
    {
        private static string GetContentType(string url)
        {
            if (url.ToLower().Contains(".css"))
            {
                return "text/css";
            }
            else if (url.ToLower().Contains(".js"))
            {
                return "application/javascript";
            }
            else
            {
                return "text/html";
            }

        }

        /// <summary>
        /// Method calls when client request the server
        /// </summary>
        /// <param name="context">HTTP context for client</param>
        public void ProcessRequest(HttpContext context)
        {

            //read values from configuration file
            int proxyMode = Convert.ToInt32(ConfigurationSettings.AppSettings["ProxyMode"]);
            string remoteWebSite = ConfigurationSettings.AppSettings["RemoteWebSite"];

            string remoteUrl;
            if (proxyMode == 0)
                remoteUrl = ParseURL(context.Request.Url.AbsoluteUri); //all site accepted
            else
                remoteUrl = context.Request.Url.AbsoluteUri.Replace("http://" + context.Request.Url.Host + context.Request.ApplicationPath, remoteWebSite); //only one site accepted

            //create the web request to get the remote stream
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(remoteUrl);

            //TODO : you can add your own credentials system 
            //request.Credentials = CredentialCache.DefaultCredentials;

            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();

            }
            catch (System.Net.WebException ex)
            {
                string dsdl;
                using (var sr = new StreamReader(ex.Response.GetResponseStream()))
                    dsdl = sr.ReadToEnd();

                //remote url not found, send 404 to client 
                context.Response.StatusCode = 404;
                context.Response.StatusDescription = "Not Found";
                context.Response.Write(dsdl);
                context.Response.End();
                return;
            }

            Stream receiveStream = response.GetResponseStream();

            context.Response.ContentType = GetContentType(remoteUrl);
            StreamReader readStream = new StreamReader(receiveStream, Encoding.Default);
            Uri test = new Uri(remoteUrl);
            string content;
            if (proxyMode == 0)
                content = ParseHtmlResponse(readStream.ReadToEnd(), context.Request.ApplicationPath + "/http//" + test.Host);
            else
                content = ParseHtmlResponse(readStream.ReadToEnd(), context.Request.ApplicationPath);
            //write the updated HTML to the client
            context.Response.Write(content);
            //close streams
            readStream.Close();
            response.Close();
            context.Response.End();

        }

        /// <summary>
        /// Get the remote URL to call
        /// </summary>
        /// <param name="url">URL get by client</param>
        /// <returns>Remote URL to return to the client</returns>
        public string ParseURL(string url)
        {
            if (url.IndexOf("http/") >= 0)
            {
                string externalUrl = url.Substring(url.IndexOf("http/"));
                return externalUrl.Replace("http/", "http://");
            }
            else
                return url;
        }

        /// <summary>
        /// Parse HTML response for update links and images sources
        /// </summary>
        /// <param name="html">HTML response</param>
        /// <param name="appPath">Path of application for replacement</param>
        /// <returns>HTML updated</returns>
        public string ParseHtmlResponse(string html, string appPath)
        {
            html = html.Replace("\"/", "\"" + appPath + "/");
            html = html.Replace("'/", "'" + appPath + "/");
            html = html.Replace("=/", "=" + appPath + "/");

            return html;
        }

        /// 
        /// Specifies whether this instance is reusable by other Http requests
        /// 
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }

控制器 HTML 未触发。还附上 Fiddler 响应。

    angulartest => Hosted angular application
    ReverseProxy => My own application

Inbox.html 没有在我的 ReverseProxy 项目中触发..

请帮帮我。

【问题讨论】:

    标签: javascript c# asp.net angularjs httphandler


    【解决方案1】:

    我终于找到了答案。反向代理期间未采用托管应用程序 Angular js 相对路径。所以我在 index.html 中添加了 CDN 版本的 angular js,

    现在它运行良好。

    【讨论】:

      猜你喜欢
      • 2021-05-22
      • 2020-07-03
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 2019-09-19
      • 1970-01-01
      • 2020-11-22
      • 2014-07-06
      相关资源
      最近更新 更多