简体   繁体   中英

how to Change the url of page asp.net?

My problem is not about url rewriting.

I want to change the url that is looking on browser.

For example the actual url is :

localhost:57358/Admin/news.aspx?Id=24

I want it to look like

localhost:57358/Admin/SomeContent....

to implement this I wrote a code in my global.asax but it is all about url rewiting.

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string sRequestedURL = Request.Path;
    string url = Request.Url.ToString();
    if (url.Contains("inner.aspx"))
    {
        int id = int.Parse(url.Split('=')[1]);
        ManageNews mn = new ManageNews();
        string title = mn.getTitleByNewsId(id);
        string targetUrl = "~/inner.aspx?Content=" + title;
        Context.RewritePath(targetUrl, false);

    }
}

please help me regarding this.

Maybe I don't really get what you are trying to do but why not use Routing ( walk through here ) to achieve what you want. This way you can specify the Route in the start of your application and let asp.net handle it.

Quote from link -

ASP.NET routing enables you to use URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users.

eg

 routes.MapPageRoute("SomeContent",
        "Admin/SomeContent"
        "Admin/news.aspx?Id=24");

If you are trying to do something far more dynamic you may be able to follow the same path used here (although that's for MVC, the logic remains the same) where the routing is configured through a database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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