从小部件生成到主网站的出站链接
我有一个位于服务器 1 上的 ASP.NET MVC 3 Web 应用程序:
我有一个小部件(也是 ASP.NET MVC 3 Web 应用程序) 位于服务器 2 上:
现在,我想生成一个指向主网站的链接从我的小部件。
因此,如果我在小部件项目中执行类似的操作:
@Html.RouteLink("Outbound link", "MainWebsiteRoute", "http", "http://www.mysite.com", string.Empty, null)
它将出错(或生成错误的链接),因为小部件和网站项目之间的路由表不同。
即使我将主网站的路线复制到小部件中,小部件项目中也不存在控制器/操作,因此它无法工作。
我该怎么做?我必须对 URL 进行硬编码吗?
I've got an ASP.NET MVC 3 web application that lives on server 1:
And i've got a widget (also ASP.NET MVC 3 web application) that lives on server 2:
Now, i want to generate a link to the main website from my widget.
So if i do something like this in the widget project:
@Html.RouteLink("Outbound link", "MainWebsiteRoute", "http", "http://www.mysite.com", string.Empty, null)
It will error (or generate the wrong link) because the route tables are different between the widget and website project.
Even if i copy the route from the main website into the widget one, the controllers/actions don't exist in the widget project, so it won't work.
How do i do it? Do i have to hardcode the URL's?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最终在小部件中复制了我需要的主网站的路线。
主网站中的示例路线:
小部件中的重复路线:
请注意没有默认路线 - 那是因为我不在乎,因为这涉及将路线与控制器/操作相匹配。但这发生在网站上,而不是小部件中。我所关心的只是生成 URL。
然后,我为
RouteLink
创建了一个名为OutboundRouteLink
的简单包装器,它指定主机名(针对主网站)并生成链接。我认为这是一个太的解决方案。显然,它在某种程度上是硬编码的 - 如果主网站中的路线发生变化,那么我将不得不更改小部件路线。但这很好,因为如果我的某个 URL 发生了更改,我无论如何都必须执行 301,因此旧的 URL 仍然适用于该小部件(它们将得到 301)。
另外,我可以更进一步,将路由 url 存储在配置文件中,并在项目之间共享它,这会给我带来更多的安全性。但只有大约 3 个出站 URL,所以这应该没问题。
并不是说这是处理它的最佳方式,但我对此感到满意。
Ended up duping the routes from the main website that i need in the widget.
Example route in main website:
Duped route in widget:
Notice how there are no route defaults - that's because i don't care, since that's concerning matching the route to a controller/action. But this happens in the website, not the widget. All i'm concerned about is generating the URL.
I then created a simple wrapper for
RouteLink
calledOutboundRouteLink
which specifies the hostname (for the main website), and generates the link.I don't think it's too bad a solution. Obviously, it's hard-coded in a way - if the routes change in the main website then i'll have to change the widget routes. But that fine, because if one of my URL's changed, i'd have to do 301's anyway, so the old URL's would still work for the widget (they would get 301'd).
Also, i could go one step further and store the route url in a config file and share that between the projects, which would give me a bit more safety. But there's only around 3 outbound URL's so this should be fine.
Not saying this is the best way to handle it, but im happy with it.
您使用区域吗?如果没有,那么也许你应该这样做。这样,您只需提供路线的区域,它就能为您解析 url。给定示例 URL,您可以将小部件站点作为一个区域来构建主站点。为路由值中的区域参数提供空字符串将允许您从小部件站点引用主站点中的控制器/操作。
Are you using areas? If not, then perhaps you should be. That way you could simply supply the area for the route and it would be able to resolve the url for you. Given your sample URLs you could build your main site with your widget site as an area. Supplying a empty string for the area parameter in the route values would allow you to reference a controller/action in the main site from the widget site.