URL重写在生产服务器上不起作用。 [英] URL Rewriting does not work on production server.

查看:55
本文介绍了URL重写在生产服务器上不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我创建了一个带URL重写的Web应用程序。它在localhost上运行得非常好。但是当上传到生产服务器时它停止了工作。



然后我读了一些IIS6中不支持无扩展URL的地方(或者IIS配置需要更改,为此我无法控制)。因此我把.aspx扩展。它开始工作(本地和生产服务器)。但现在在生产服务器CSS& JavaScript文件未加载但在localhost上正常工作。



我使用的是Global.asax&我自己的URL重写模块。



Global.asax文件代码:

Hi all,

I have created a web application with URL Rewriting. It''s working absolutely fine on localhost. But when uploaded to production Server it stopped working.

Then I read some where that extentionless URLs are not suppoeted in IIS6 (or IIS configuration needs to be changed, and for that I have no control). Therefore I put .aspx extention. And it start working (both locally & on Production server). But now on Production server CSS & JavaScript Files are not loading but on localhost its working correctly.

I am using Global.asax & my own module for URL Rewriting.

Code on Global.asax file:

void Application_AcquireRequestState(object sender, EventArgs e)
{
    //Session is Available here
    string strLastURL = "";
    string fullOrigionalpath = HttpContext.Current.Request.Url.ToString().ToLower();

    if (fullOrigionalpath.Contains(".aspx")  && !fullOrigionalpath.Contains("404error.aspx"))
    {//Only .aspx files to be processed

        if (HttpContext.Current != null && HttpContext.Current.Session != null)
        {
            if (HttpContext.Current.Session["LastAccessURL"] != null)
                strLastURL = HttpContext.Current.Session["LastAccessURL"].ToString();
            else
                HttpContext.Current.Session["LastAccessURL"] = strLastURL = fullOrigionalpath;
        }

        if (fullOrigionalpath.Contains("/example.aspx") && fullOrigionalpath != strLastURL)
        {
            HttpContext.Current.Session["LastAccessURL"] = fullOrigionalpath;
            HttpContext.Current.Response.Redirect("~/ChangedByUrl/Example.aspx");
        }

        try
        {
            HttpContext.Current.Session["LastAccessURL"] = fullOrigionalpath;
        }
        catch { }
    }
}











重写模块代码:






Rewrite Module Code:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for RewriteURL
/// </summary>
public class RewriteURL : IHttpModule
{
    public RewriteURL()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public void Init(HttpApplication application)
    {
        application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
    }

    public void Dispose()
    {
    }

    private void Application_Disposed(Object source, EventArgs e)
    {

    }

    void Application_BeginRequest(object sender, EventArgs e)
    {
        string fullOrigionalpath = HttpContext.Current.Request.Url.ToString().ToLower();

        if (fullOrigionalpath.EndsWith("/changedbyurl/example.aspx"))
        {
            HttpContext.Current.RewritePath("~/Example.aspx");
        }
        else if (fullOrigionalpath.Contains("/changedbyurl/"))
        {
            //This is to correctly load the CSS & Java scrit files or Images etc on page.
            if (fullOrigionalpath.Contains("/changedbyurl"))
            {
                int start = 0;
                int end = 0;
                start = fullOrigionalpath.IndexOf("/changedbyurl");
                end = start + "/changedbyurl".Length;
                string strSubString = fullOrigionalpath.Substring(0, start);
                strSubString += "~" + fullOrigionalpath.Substring(end);
                fullOrigionalpath = strSubString;
            }

            string[] arr = fullOrigionalpath.Split('~');
            if (arr.Length > 1)
            {
                HttpContext.Current.RewritePath("~/" + arr[arr.Length - 1]);
            }
        }

    }
}







Web.config:

< system.web>



< httphandlers>

< remove verb =*path =* .asmx>

< add verb =*path =*。asmxvalidate =falsetype =System .Web.Script.Services.ScriptHandlerFactory,System.Web.Extensions,Version = 1.0.61025.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35>

< add verb =*path = * _AppService.axdvalidate =falsetype =System.Web.Script.Services.ScriptHandlerFactory,System.Web.Extensions,Version = 1.0.61025.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35>

< add verb =GET,HEADpath =ScriptResource.axdtype =System.Web.Handlers.ScriptResourceHandler,System.Web.Extensions,Version = 1.0.61025.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35 validate =false>



< httpmodules>

< add name =rewriteurl type =RewriteURL>

< add name =ScriptModuletype =System.Web.Handlers.ScriptModule,System.Web.Extensions,Version = 1.0.61025.0,Culture = neutral, PublicKeyToken = 31bf3856ad364e35>








Web.config:
<system.web>

<httphandlers>
<remove verb="*" path="*.asmx">
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false">

<httpmodules>
<add name="rewriteurl" type="RewriteURL">
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">


<system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        <modules>
      <add name="rewriteurl" type="RewriteURL"/>
            <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        </modules>
        <handlers>
            <remove name="WebServiceHandlerFactory-Integrated"/>
            <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        </handlers>
    </system.webServer>







这对本地主机非常完美,但在生产(有IIS6)CSS& JavaScript文件未加载。



在head标签内的主页面上:




This working absolutely perfectly on local host but on production (having IIS6) CSS & JavaScript files are not loading.

On Master page inside head tag:

<link rel="stylesheet" href="jquery.megamenu.css" type="text/css" media="screen" />
    <link href="style.css" rel="stylesheet" type="text/css" />
    <link href="popup-div.css" rel="stylesheet" type="text/css" />
    <link href="style1.css" rel="stylesheet" type="text/css" />





a bove CSS& Javascript文件在本地主机上正确加载,但在生产服务器上没有。我不想手动修复它们的路径作为绝对路径。我想知道为什么会这样?





后来添加-------------- --------

解决CSS&我使用以下方法的Javascript文件问题:



The above CSS & Javascript files are loading correctly on Local host but not on production server. I do not want to manually fix the paths for them as absolute path. I wonder why this is happening?


Added Later----------------------
To solve the CSS & Javascript files issue following approach is used by me:

protected void Page_Init(object sender, EventArgs e)
    {
        if (ViewState["OnceCalled"] != null)
            return;
        ViewState["OnceCalled"] = "Yes";
        objUtility.SetCssVersion(Page);
    }
    private void SetCssVersion(Control objHead)
    {
        foreach (Control c in EnumerateControlsRecursive(objHead))
        {
            if (c is HtmlLink)
            {
                HtmlLink lnkStylesheet = (HtmlLink)c;
                if (lnkStylesheet != null)
                {
                    if (lnkStylesheet.Href.Contains("?"))
                        lnkStylesheet.Href += "&" + Version();
                    else
                        lnkStylesheet.Href += "?" + Version();

                    if (!lnkStylesheet.Href.ToLower().Contains(".com") || !lnkStylesheet.Href.ToLower().Contains("http:"))
                    {
                        lnkStylesheet.Href = "Website name here/" + lnkStylesheet.Href;
                    }
                }
            }
        }
    }



但仍未加载图片。

我不认为这种方法可以很好地使所有图像的URL绝对在页面上。



或者那里''处理这个问题的任何其他方法??

以后添加的结束----------------------



任何想法??



谢谢

Ashish


But still Images are not loading.
I don''t think this approach is good to make all image''s URL absolute on page.

Or there''s any other approach to handle this issue??
Added Later Ends----------------------

Any ideas??

Thanks
Ashish

推荐答案

但正在制作(拥有IIS6)CSS& JavaScript文件未加载

听起来好像是相对路径问题。有时,在部署之后,未正确形成作为图像,样式表等的源引用的路径。这导致在所引用的位置找不到文件,因为哪些图像无法下载或样式表未被应用。

您需要确保路径是正确的,例如文件找到并使用。



我建议您在设置文件来源之前使用此提示并正确解析路径:在多文件夹网站中解析路径 [ ^ ]
but on production (having IIS6) CSS & JavaScript files are not loading
Sounds like it is relative path issue. At times, after deployment the path referenced as a source for images, stylesheets, etc is not correctly formed. This leads to no file found at the location referred because of which images don''t download or style-sheets does not get applied.
You would need to make sure that the path is correct such that file is found and used.

I would suggest you to use this Tip and resolve the path correctly before setting the source of the file: Resolving Paths in a Multi-Folder WebSite[^]


这篇关于URL重写在生产服务器上不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆