简体   繁体   English

Url在Asp.net中由global.asax和错误404重写

[英]Url rewrite by global.asax and Error 404 in Asp.net

I am working on a project with following properties : 我正在开发一个具有以下属性的项目:

Asp.net
C# 
.Net Framework 4
IIS 7

and in it , I use the Application_BeginRequest to rewrite manually like this : 在其中,我使用Application_BeginRequest手动重写,如下所示:

void Application_BeginRequest(object sendet, EventArgs e)
    {

        bool IsUploading = System.Configuration.ConfigurationManager.AppSettings["IsUloading"] == "1";

        if (Request.Headers["X-Requested-With"] == "XMLHttpRequest")
        {
            return;
        }

        string Path = Request.RawUrl;
        string QueryString = "";

        if (Request.QueryString.Count != 0)
        {
            string[] u = Path.Split(new char[] { '?' });
            Path = u[0];
            QueryString = u[1];
        }

        string url = "";

        if (Path.EndsWith(".css") || Path.EndsWith(".js") || Path.EndsWith(".png") || Path.EndsWith(".jpg") || Path.EndsWith(".jpeg") || Path.EndsWith(".bmp") || Path.EndsWith(".htm") || Path.EndsWith(".png") || Path.EndsWith(".gif") || Path.EndsWith(".fla") || Path.EndsWith(".swf") || Path.EndsWith(".axd") || Path.EndsWith(".xml") || Path.EndsWith(".ashx"))
            return;

        if (!Path.EndsWith("/"))
            Path += "/";

        IEnumerator<string> Part = Path.Split(new char[] { '/' }).AsEnumerable<string>().GetEnumerator();

        if (Part.MoveNext() && Part.MoveNext())
        {

            if (Part.Current == "")
                url = "Default.aspx";

            else if (Part.Current.ToLower() == "public")
            {

                if (Part.MoveNext() && Part.Current.ToLower() == "install")
                    url = "Public/Install.aspx";
                else
                    url = "Public/PageNotFound.aspx";

            }

            else
                url = "Public/PageNotFound.aspx";

        }
        else
            url = "Public/PageNotFound.aspx";

        if (url.Contains("?"))
            Context.RewritePath("~/" + url + "&" + QueryString);
        else
            Context.RewritePath("~/" + url + "?" + QueryString);

    }

now when try to browse the page localhost:97 , browser start to open the Default.aspx but if I try to open localhost:97/Public/Install the browser give me this error : 现在当尝试浏览localhost:97页面时,浏览器开始打开Default.aspx但是如果我尝试打开localhost:97/Public/Install浏览器给我这个错误:

HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

Where is the error ?!!! 错误在哪里?!!!

UPDATE1 : When I try to brows localhost:97/Public/Install , VS does not stopped on the breakpoints that I put in this event. UPDATE1 :当我尝试浏览localhost:97/Public/Install ,VS没有停止在我放入此事件的断点上。 I think my request was rejected in IIS. 我认为我的请求在IIS中被拒绝了。

UPDATE2 : I know that my code is one hundred percent correct, because we are working on TFS server and the other computer does not have any problem with that and just my computer encountered with this problem. UPDATE2 :我知道我的代码百分之百正确,因为我们正在使用TFS服务器而另一台计算机没有任何问题,只是我的计算机遇到了这个问题。

Sorry about my bad English. 抱歉我的英语不好。 I am new. 我是新的。 For more detail comment me. 有关详细信息,请评论我。

i think it could be great using aspnet routing to achive your goal. 我认为使用aspnet路由实现目标可能会很棒。

you can create your own path eg "public/install" which is one route. 你可以创建自己的路径,例如“public / install”,这是一条路线。

If it is match aspnet move there also you can use a catch all route which is use to show a 404 403 500 and so on.more simple and reduce your code drammatically. 如果它匹配aspnet移动那里你也可以使用catch所有路由用于显示404 403 500等等。更简单并且戏剧性地减少你的代码。

it is a simple example in vbnet: 这是vbnet中的一个简单示例:

  Sub RegisterRoutes(ByVal routes As RouteCollection)
    routes.EnableFriendlyUrls()
    'blocca il trattamento dei vari snippet
    routes.Add(New Route("{resource}.axd/{*pathInfo}", New StopRoutingHandler()))
    routes.Add(New Route("route", New StopRoutingHandler()))
    routes.Add(New Route("bundles/css", New StopRoutingHandler()))
    'routes.Add(New Route("script/zx", New StopRoutingHandler()))
    'routes.Add(New Route("script/av", New StopRoutingHandler()))
    'routes.Add(New Route("script/pf", New StopRoutingHandler()))
    'routes.Add(New Route("script/bv", New StopRoutingHandler()))
    'sezione errori
    routes.MapPageRoute("404", "NotFound", "~/error/404.aspx", False)
    routes.MapPageRoute("500", "ServerError", "~/error/500.aspx", False)
    routes.MapPageRoute("503", "ServerUnavailable", "~/error/503.aspx", False)
    'fonti base
    routes.MapPageRoute("Home", "", "~/Default.aspx", False)
    routes.MapPageRoute("DefaultVasVoip", "vas-e-voip", "~/route/vas-voip/DefaultVasVoip.aspx", False, New RouteValueDictionary(New With {.name = "default"}))
    routes.MapPageRoute("VasVoipRoute", "vas-e-voip/{service}/{id}", "~/route/vas-voip/DefaultVasVoip.aspx", False, New RouteValueDictionary(New With {.name = "VasVoipRoute"}))
    routes.MapPageRoute("MarketingRoute", "marketing/{service}/{id}", "~/route/marketing/DefaultMarketing.aspx", False, New RouteValueDictionary(New With {.name = "MarketingRoute"}))
    routes.MapPageRoute("DefaultMarketing", "marketing", "~/route/marketing/DefaultMarketing.aspx", False, New RouteValueDictionary(New With {.name = "default"}))
    routes.MapPageRoute("HostingRoute", "hosting/{service}/{id}", "~/route/hosting/DefaultHosting.aspx", False, New RouteValueDictionary(New With {.name = "HostingRoute"}))
    routes.MapPageRoute("Optimization", "seo/{service}/{id}", "~/route/seo/DefaultSeo.aspx", False, New RouteValueDictionary(New With {.name = "Optimization"}))
    routes.MapPageRoute("DefaultSeoHosting", "hosting-e-seo", "~/route/hosting/DefaultHosting.aspx", False, New RouteValueDictionary(New With {.name = "default"}))
    routes.MapPageRoute("Contact", "contatti", "~/route/Contact.aspx", False)
    routes.MapPageRoute("Login", "login", "~/route/ssl/login.aspx", False, New RouteValueDictionary(New With {.name = "login", .ssl = True}))
    routes.MapPageRoute("LoginRedirect", "login/redirect", "~/route/ssl/login.aspx", False, New RouteValueDictionary(New With {.name = "redirect"}))
    routes.MapPageRoute("Admin", "ssl/admin/", "~/route/ssl/private/admin/Default.aspx")
    routes.MapPageRoute("Customer", "ssl/customer/", "~/route/ssl/private/customer/Default.aspx")
    routes.MapPageRoute("About", "chi-siamo", "~/route/About.aspx", False)
    routes.MapPageRoute("articles", "articles", "~/route/article/Default.aspx", False)
    routes.MapPageRoute("article", "article/{gg}/{mm}/{yyyy}/{id}/{title}", "~/route/article/Default.aspx", False)
    routes.MapPageRoute("termini", "termini-e-condizioni", "~/route/Termini.aspx", False)
    routes.MapPageRoute("privacy", "privacy", "~/route/Privacy.aspx", False)
    routes.MapPageRoute("portfolio", "portfolio", "~/route/portfolio.aspx", False)
    routes.MapPageRoute("faq", "faq", "~/route/faq.aspx", False)
    'Catch All per ogni richiesta errata 
    routes.MapPageRoute("CatchAllErrors", "{*url}", "~/InvalidRequest.aspx", False)
End Sub

I hope it could help you. 我希望它可以帮助你。

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

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