简体   繁体   中英

ASP.net MVC routing parameters

I have route which looks like this:

http://localhost:1936/user/someOne/##feed%233dbfc1b3-44b4-42b4-9305-4822ac151527

My routes are configured like this:

   routes.MapRoute("Profile", "user/{userName}/{treningId}",
            new { controller = "Profile", action = "Index", userName = UrlParameter.Optional, treningId = UrlParameter.Optional }
            );

And Action in my controller like this:

public ActionResult Index(string username, string treningId)
    {
      //more code here
    }

But I can't get this part: ##feed%233dbfc1b3-44b4-42b4-9305-4822ac151527 , when I debugged code, I saw treningId was null.

My question is how can I get part of URL after ## ?

This is not possible as hashes are not included in the request; they are reserved for client-side use in URLs (typically to mark a specific location in/portion of a document).

You have two options. Either encode the hash character as %23 :

%23%23feed%233dbfc1b3-44b4-42b4-9305-4822ac151527

Or use a different character/route.

You can't.

The browser doesn't send the stuff after the first # to the server at all (you can verify this by using the debugger in your browser to inspect the traffic between your browser and the server). That is because the # (hash) character carries special meaning in HTTP .

Why don't you just

  • use regular query parameters (so, something like http://localhost:1936/user/someOne?myparameter=feed%233dbfc1b3-44b4-42b4-9305-4822ac151527 ), or, alternatively,
  • choose another delimiter than "##".

The way you have your URL written, the browser would not submit any portion of the fragment identifier - the hash part of the URL. As such, your MVC app will never receive the value and thus will always have it as null .

To make that value available to MVC, escape the # to avoid them marking beginning of fragment portion of URL.

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