简体   繁体   English

如何使用System.Web.Routing而不在Web窗体中重写URL?

[英]How to use System.Web.Routing to not URL rewrite in Web Forms?

I am using System.Web.Routing with ASP.NET (3.5) Web Forms that will URL rewrite the following URL from 我正在将System.Web.Routing与ASP.NET(3.5)Web窗体一起使用,它将通过URL重写以下URL

to

The code is as below: 代码如下:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.Add("CampaignRoute", new Route
                                    (
                                    "{campaign_code}",
                                    new CustomRouteHandler("~/default.aspx")
                                    ));
}

IRouteHandler implementation: IRouteHandler的实现:

public class CustomRouteHandler : IRouteHandler
{
    public CustomRouteHandler(string virtualPath)
    {
        VirtualPath = virtualPath;
    }

    public string VirtualPath { get; private set; }

    public IHttpHandler GetHttpHandler(RequestContext
          requestContext)
    {
        if (requestContext.RouteData.Values.ContainsKey("campaign_code"))
        {
            var code = requestContext.RouteData.Values["campaign_code"].ToString();

            HttpContext.Current.RewritePath(
                string.Concat(
                    VirtualPath,
                    "?campaign=" + code));
        }

        var page = BuildManager.CreateInstanceFromVirtualPath
                       (VirtualPath, typeof(Page)) as IHttpHandler;

        return page;
    }

However I noticed there are too many things to change on my existing aspx pages (ie links to javascript, links to css files). 但是,我注意到在现有的aspx页面上有太多事情需要更改(即,指向javascript的链接,指向css文件的链接)。

  1. So I am thinking if there's a way to keep above code but in the end rather than a rewrite just do a Request.Redirect or Server.Transfer to minimize the changes needed. 因此,我在考虑是否有一种方法可以保留上面的代码,但是最后而不是重写,只需执行Request.Redirect或Server.Transfer即可最大程度地减少所需的更改。 So the purpose of using System.Web.Routing becomes solely for URL friendly on the first entry. 因此,使用System.Web.Routing的目的仅是为了第一个条目上的URL友好。

  2. How to ignore the rest of the patterns other than specificed in the code? 如何忽略代码中未指定的其他模式?

Thanks. 谢谢。

Using rewriting combined with ASP.NET URL Routing is not recommended because some implementations of ASP.NET URL Routing internally use rewriting as well (it depends on the version of ASP.NET). 不建议将重写与ASP.NET URL路由结合使用,因为ASP.NET URL路由的某些实现在内部也使用重写(取决于ASP.NET的版本)。 The combination of two different components using rewriting can cause conflicts (though I'm not 100% sure that that's why you're seeing this problem). 使用重写将两个不同的组件结合在一起会导致冲突(尽管我不是100%确信这就是为什么您遇到此问题的原因)。

Regarding using transfer/redirect/rewrite: 关于使用传输/重定向/重写:

My strongest recommendation would be to not use any of them! 我最强烈的建议是不要使用任何一个! Instead of redirecting (or anything else) just let the page be called directly by ASP.NET by returning it from the IRouteHandler, much as you are already doing (just without the call to Rewrite). 无需重定向(或其他任何操作),只需让ASP.NET通过从IRouteHandler返回它来直接调用该页面即可,就像您已经在做的一样(只是不调用Rewrite)。 As long as your IRouteHandler saves the RouteData somewhere, the Page can then get the data from the route and you should be good to go. 只要您的IRouteHandlerRouteData保存在某个位置,Page便可以从路由中获取数据,您应该一切顺利。

Take a look at Phil Haack's Web Form routing sample to see an example of how to save the route data (or just use his code!). 看一下Phil Haack的Web表单路由示例 ,看看如何保存路由数据(或仅使用他的代码!)的示例。

Regarding ignoring patterns: 关于忽略模式:

You can use an IRouteConstraint to constrain which URLs match your route. 您可以使用IRouteConstraint来限制哪些URL与您的路由匹配。 There is a built-in default route constraint implementation that uses regular expressions, but you can also write custom route constraints. 有一个内置的默认路由约束实现,该实现使用正则表达式,但您也可以编写自定义路由约束。 Here is an example: 这是一个例子:

Route r = new Route(...);
r.Constraints = new RouteValueDictionary(new {
    campaign_code = "\d{5}", // constrain to 5-digit numbers only
    other_value = new CustomRouteConstraint(), // call custom constraint
});

CustomRouteConstraint is a class that you can write that derives from IRouteConstraint . CustomRouteConstraint是您可以编写的类,该类派生自IRouteConstraint

One thing I should note about static files such as CSS and JPG files is that by default they are always excluded from routing. 关于静态文件(例如CSS和JPG文件),我应该注意的一件事是,默认情况下,始终将它们排除在路由之外。 By default routing ignores patterns that exactly match physical files on disk. 默认情况下,路由会忽略与磁盘上的物理文件完全匹配的模式。 You can change this behavior by setting RouteTable.Routes.RouteExistingFiles = true , but that is not the default. 您可以通过设置RouteTable.Routes.RouteExistingFiles = true来更改此行为,但这不是默认设置。

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

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