开发者

page.aspx/variable in ASP.NET

开发者 https://www.devze.com 2023-01-09 01:18 出处:网络
Is is possible to pass/get variables like page.aspx/value in ASP.NET? I\'d like to have a page like website.com/folder/name instead of website.com/folder/?name and get that name value.开发者_C百科I th

Is is possible to pass/get variables like page.aspx/value in ASP.NET? I'd like to have a page like website.com/folder/name instead of website.com/folder/?name and get that name value.开发者_C百科


I think you can use ASP.Net routing: http://msdn.microsoft.com/en-us/library/cc668201.aspx or URL rewriting: http://www.codeproject.com/kb/aspnet/URLRewriter.aspx

difference (from MSDN):

ASP.NET Routing versus URL Rewriting

ASP.NET routing differs from other URL rewriting schemes. URL rewriting processes incoming requests by actually changing the URL before it sends the request to the Web page. For example, an application that uses URL rewriting might change a URL from /Products/Widgets/ to /Products.aspx?id=4. Also, URL rewriting typically does not have an API for creating URLs that are based on your patterns. In URL rewriting, if you change a URL pattern, you must manually update all hyperlinks that contain the original URL.

With ASP.NET routing, the URL is not changed when an incoming request is handled, because routing can extract values from the URL. When you have to create a URL, you pass parameter values into a method that generates the URL for you. To change the URL pattern, you change it in one location, and all the links that you create in the application that are based on that pattern will automatically use the new pattern.automatically use the new pattern.


A good source for more information is here


I've just done something similar recently with routing to allow for dates to be passed through for blog entries and stuff.

To do it for your setup you will need to have a page which will do all the processing, lets call that folder.aspx and then add in the route information to your Global.ascx.cs file.

So lets look at the routes ...

Routes

Routes need to work from specific and work there way out to general. This way the request will be able to find the most specific version which applies to url specified. The following code needs to be added to your Global.ascs.cs file.

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        SetRoutes(RouteTable.Routes);
    }

    private static void SetRoutes(RouteCollection routes)
    {

        routes.MapPageRoute("folder-file",
                            "{folder}/{file}",
                            "~/folder.aspx");

        routes.MapPageRoute("folder",
                            "{folder}",
                            "~/folder.aspx");
    }

One thing to note is you might need to sort out other routes in addition to this as this will intercept other sub folder references.

Right, now the routes are sorted you can then access the route values in the code behind of the folder.aspx page.

Page code behind

    protected void Page_Load(object sender, EventArgs e)
    {
        var folder = Page.RouteData.Values["folder"] as string;
        var file = Page.RouteData.Values["file"] as string;

        lblFolder.Text = string.IsNullOrEmpty(folder) ? "nothing" : folder;
        lblFile.Text = string.IsNullOrEmpty(file) ? "nothing" : file;
    }

The above code is just an example as to how you could access route values, and then I've just assigned them to some labels checking for null or empty :-)

So to access the folder.aspx page you can now browse to domain/folder/name or domain/folder .

Hope this helps.

0

精彩评论

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