简体   繁体   中英

URL Rewriting in C#

I noticed similar questions to what I have to ask but I feel I haven't really had my question answered.

I created an admin panel for a client. He has the ability to create Destinations (ie Rome, Barcelona, Cancun)

All the information for each destination is dynamic and currently looks like this:

/Destination.aspx?id=1

I would like it to look like this:

/Rome/ or /Cancun/

Is this plausible?

Yes, it's quite possible. If you use the routing feature in ASP.NET , then it actually pretty easy to get it working (routing is not just for MVC, you know!)

In Global.asax.cs:

protected void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("",
        "Destinations/{name}",
        "~/Destinations.aspx");
}

Then in your destinations page, you access the parameter like so:

private void Destinations_Load(object sender, EventArgs e)
{
    string destinationName = Convert.ToString(RouteData.Values["name"]);
    // load destination with name destinationName...
}

Check the ASP MVC Framework here . With the MVC framework is very easy to have a RESTful interface. With the framework you could be able to use URL "directories" as parameters.

You could be able to have something like /Vacation/Places/Cancun and under the Vacation controller you could have a method like:


 public void Places(string place)
 {
     if(place.Equals("Cancun"))
     {
              return View("Cancun");
     }
     else if(place.Equals("Puerto Rico"))
     {
         .......
     }
     ...............
}

您应该使用ASP.Net Routing ,它可以独立于MVC框架使用。

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