将网站URL传递到ASP.NET MVC中的Route

时间:2022-02-08 15:11:56

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.

我正在开发一个项目,需要获取实际的URL并在路由本身中使用它。我所看到的一切,阅读和测试都允许路线在网站URL之后抓取项目。这是我尝试过的,每次只是说网站是空白的(这可能是因为默认为空白)。你可以告诉我任何骨头允许路由也使用根URL吗?目标是一个网站,它将处理URL并在视图内部将我路由到不同的内容。

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

这是我尝试过的代码,但在localhost之外没有运气

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

Your help is greatly appreciated. Thanks in advance

非常感谢您的帮助。提前致谢

2 个解决方案

#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?

如何在控制器的动作中使用HttpContext.Request.RawUrl。我可能在这里遗漏了一些东西。您想要阅读的网址来自哪里?是当前页面还是其他页面?

#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.

内森的答案是答案的一部分。 HttpContext.Request.RawUrl允许访问此信息,但在Global.asax.cs文件中。这是在Application_BeginRequest事件中完成的。但是诀窍(如果有人知道更优雅的方式,请发表评论)但是我们将使用的Url注入到剩余的路径路径中,以便可以在MapRoute方法中获取它。因此,“http://subdomain.domain.net/url”网站将显示为“http://subdomain.domain.net/subdomain.domain.net/url,因此可以在{site}部分中选择它MapRoute命令。这不会向用户显示,但允许我们根据收到的URL在应用程序内做出决定。这听起来是否合理?如果这似乎是一种疯狂的方式来吸收根网址。

#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?

如何在控制器的动作中使用HttpContext.Request.RawUrl。我可能在这里遗漏了一些东西。您想要阅读的网址来自哪里?是当前页面还是其他页面?

#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.

内森的答案是答案的一部分。 HttpContext.Request.RawUrl允许访问此信息,但在Global.asax.cs文件中。这是在Application_BeginRequest事件中完成的。但是诀窍(如果有人知道更优雅的方式,请发表评论)但是我们将使用的Url注入到剩余的路径路径中,以便可以在MapRoute方法中获取它。因此,“http://subdomain.domain.net/url”网站将显示为“http://subdomain.domain.net/subdomain.domain.net/url,因此可以在{site}部分中选择它MapRoute命令。这不会向用户显示,但允许我们根据收到的URL在应用程序内做出决定。这听起来是否合理?如果这似乎是一种疯狂的方式来吸收根网址。