How to specify url mappings in ASP.NET 4.0 when parameters are involved?

后端 未结 2 1957
时光取名叫无心
时光取名叫无心 2021-01-25 17:12

The problem

I have a web site with some articles inside. Articles are accessed using a database writing results on a single page. When a certain article is to be access

相关标签:
2条回答
  • 2021-01-25 17:32

    Details: Would you be fine with

    http://www.mysite/Articles?a={article-id}

    If so Microsoft Friendly Urls would be good for you. You would not need to change any of your existing functionality, you would just need to add the package and then add one line to Global.asax file. Also all your other Url's would be clean (i.e. http://www.mysite/Articles, http://www.mysite/About). It works for Web Site/Forms but does not work for Web Pages (i.e. Web Matrix)

    Solution:

    1. In solution Explorer, right click on your Website and select "Manage Nuget Packages.."
    2. Press the drop down that says "Stable Only" and choose "Include Prerelease" (it is in Pre-Release)
    3. In the search box enter "Microsoft.AspNet.FriendlyUrls"
    4. Select "Install"
    5. Add a Global Application Class "Global.asax"
    6. In the Application_Start method add the following line

      RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes);

    7. You will now have FriendlyURL's

    Note: If you are using a web site you will need to delete the following files for it to work (It will work fine with Web forms. Once it becomes a stable release I am sure you wont need to delete these files)

    • Site.Mobile.Master
    • Site.Mobile.Master.designer.cs
    • ViewSwitcher.ascx
    • ViewSwitcher.ascx.designer.cs


    Links To More Information:

    Information on Microsoft's Friendly Url Package

    0 讨论(0)
  • 2021-01-25 17:38

    I think you should use:

    1. ASP.NET Routing

    Eg.:

    protected void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }
    
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("Articles",
            "articles/{article-id}/",
            "~/Articles.aspx");
    }
    

    & on "Articles.aspx.cs" you can get the "article-id" using:

    string article-id = Page.RouteData.Values["article-id"] as string;
    
    1. Using the URL Rewrite Module

    enter image description here


    1. urlrewriter.net

    enter image description here

    0 讨论(0)
提交回复
热议问题