简体   繁体   中英

Reverse Proxy Not working for Angular App

Hi I'm trying to open hosted angular app to my application without using iFrame , Object and embed tags. Below is my Handler code. Css and js files are loaded properly but site is not working as expected.

**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;
            }
        }
    }

Controller HTML is not firing. Attached Fiddler response also.

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

在此处输入图片说明

Inbox.html not firing in my ReverseProxy project..

Please help me for this.

Finally I found the answer. Hosted application angular js relative path not taking during Reverse Proxy. So I added in CDN version of angular js in index.html,

Now it's working perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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