Tutorial :Pass website URL into the Route in ASP.NET MVC



Question:

I am working on a project that needs to grab the actual URL and use it in the route itself. Everything I am seeing, reading and testing allows the route to grab the items after the site URL. This is what I have tried and each time it simply say the site is blank (which is probably because of the default of blank). Can you throw me any bones on allowing the route to also utilize the root URL? The goal is one website that will process the URL and route me to different guts once inside of the View.

Here is code I have tried but no luck outside of localhost

            routes.MapRoute(              "PageRouter",              "{site}/{*url}",              new { controller = "PageRouter", action = "RoutePage", site = "", url = "" }          );  

Your help is greatly appreciated. Thanks in advance


Solution:1

How about just using HttpContext.Request.RawUrl inside the action of the controller. I may be missing something here. Where is the url you want to read coming from? Is it the current page or another page?


Solution:2

Nathan's answer is part of the answer. The HttpContext.Request.RawUrl allows access to this info but in the Global.asax.cs file instead. This being done in the Application_BeginRequest event. But the trick (and please comment if someone knows a more elegant way to do this) but we inject the Url being used into the remaining route path so it can be picked up in the MapRoute method. So the "http://subdomain.domain.net/url" site would then appear as "http://subdomain.domain.net/subdomain.domain.net/url so it can be picked up in the {site} part of the MapRoute command. This would not be shown to the user but then allows us to make decisions inside the app based on received URL. Does this sound sane? Comments are welcome if this seems a crazy way of doing it in order to absorb the root Url.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »