简体   繁体   English

用C#重写URL

[英]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 /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!) 如果在ASP.NET中使用路由功能 ,则实际上很容易使它起作用(路由不仅仅适用于MVC,您知道!)

In Global.asax.cs: 在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 . 此处检查ASP MVC框架。 With the MVC framework is very easy to have a RESTful interface. 使用MVC框架很容易拥有RESTful接口。 With the framework you could be able to use URL "directories" as parameters. 使用该框架,您可以将URL“目录”用作参数。

You could be able to have something like /Vacation/Places/Cancun and under the Vacation controller you could have a method like: 您可能可以拥有/ Vacation / Places / Cancun之类的东西,并且在Vacation控制器下,您可以拥有如下方法:


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

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

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

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