开发者

rewrite url. asp.net c# [closed]

开发者 https://www.devze.com 2022-12-21 15:34 出处:网络
It's 开发者_StackOverflow社区difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its curr
It's 开发者_StackOverflow社区difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

how to rewrite url string. in asp.net with c#.net.


ASP.NET supports URL rewriting via System.Web.Routing, it is not just for ASP.NET MVC.

See How to: Use Routing with Web Forms on MSDN.

To have URL ~/foo handled by page ~/example/foo.aspx register the route in global.asax.cs

void Application_Start(object sender, EventArgs e)
{
    Route r = new Route("{Parameter}", new ExampleRouteHandler());
    Routes.Add(r);
}

And the route handler might look like this:

public class ExampleRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        string page = requestContext.RouteData.GetRequiredString("Parameter");

        if (page == "") {
            page = "default";
        }

        string @virtual = string.Format("~/example/{0}.aspx", page);

        return (Page)BuildManager.CreateInstanceFromVirtualPath(@virtual, typeof(Page));
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消