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

时间:2012-07-25 18:31:38

标签: asp.net iis-7 url-rewriting

我正在开发一个具有以下属性的项目:

Asp.net
C# 
.Net Framework 4
IIS 7

在其中,我使用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);

    }

现在当尝试浏览页面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.

错误在哪里?!!!

UPDATE1 :当我尝试浏览localhost:97/Public/Install时,VS没有停在我放入此事件的断点上。我认为我的请求在IIS中被拒绝了。

UPDATE2 :我知道我的代码百分之百是正确的,因为我们正在使用TFS服务器而另一台计算机没有任何问题,只是我的计算机遇到了这个问题。

抱歉我的英语不好。我是新的。有关详细信息,请评论我。

1 个答案:

答案 0 :(得分:0)

我认为使用aspnet路由实现目标可能会很棒。

你可以创建自己的路径,例如&#34; public / install&#34;这是一条路线。

如果它匹配aspnet移动那里你也可以使用catch所有路径用于显示404 403 500等等。更简单并且戏剧性地减少你的代码。

这是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

我希望它可以帮到你。