如何更改 MVC 动态呈现操作链接的方式?
我需要根据配置设置更改 MVC 呈现操作链接(以及表单和 url 等)的方式。 我正在使用 MS MVC 编写 Facebook 应用程序,我的操作链接需要渲染链接,因此:
<a href="/MyFBApplication/Home/Index/">home</a>
单击上面的链接将浏览到:
http://apps.facebook.com/MyFBApplication/Home/Index/
“MyFBApplication”是 facebook 应用程序的名称。 为了构建链接,我首先调用
Html.ActionLink("Home", "Index", "Home")
当用户单击链接时,facebook 将在幕后向此链接发出请求:
http://www.myapplicationserver.com/facebook/123456/Home/Index/
(123456 是客户端 ID,是必需的)
然后来自此处的响应由 facebook 处理并显示给用户:
http://apps.facebook.com/MyFBApplication/Home/Index/
正如上面提到的,facebook 在后台请求页面数据。
http://www.myapplicationserver.com/facebook/123456/Home/ Index/
问题是调用
Html.ActionLink("Home", "Index", "Home")
将呈现导致
的链接 http://apps.facebook.com/facebook/123456/Home/ Index/
我需要它来引导:
http://apps.facebook.com/MyFBApplication/Home/Index/
我确实通过构建一组新的扩展方法使其正常工作。
Html.FacebookActionLink("Home", "Index", "Home")
Html.FacebookBeginForm("Index", "Home")
Html.Facebook(etc)
但这有一种“气味”。 我想找到一个可以拦截进程中的 Html.ActionLink() 管道并更改输出的地方。 然后我就可以使用所有标准调用了。
谢谢!
I need to change the way MVC is rendering action links(and form's and url's, etc) based on a configuration setting. I am writing a facebook application using MS MVC and my action links need to render link so:
<a href="/MyFBApplication/Home/Index/">home</a>
clicking the above link would browse to:
http://apps.facebook.com/MyFBApplication/Home/Index/
"MyFBApplication" is the name of the facebook application.
To build the link I started by calling
Html.ActionLink("Home", "Index", "Home")
When a user click the link facebook will then make a request to this link under the covers:
http://www.myapplicationserver.com/facebook/123456/Home/Index/
(123456 is client id and is needed)
Then response from here is processed by facebook and displayed to the user as:
http://apps.facebook.com/MyFBApplication/Home/Index/
As mentioned above facebook request the page data behind the scenes from.
http://www.myapplicationserver.com/facebook/123456/Home/Index/
The problem is a call to
Html.ActionLink("Home", "Index", "Home")
will render a link that leads to
http://apps.facebook.com/facebook/123456/Home/Index/
and I need it to lead to:
http://apps.facebook.com/MyFBApplication/Home/Index/
I did get it work properly buy building a new set of extension methods.
Html.FacebookActionLink("Home", "Index", "Home")
Html.FacebookBeginForm("Index", "Home")
Html.Facebook(etc)
But this has a "smell" to it. I would like to find a place where I can intercept the plumbing of Html.ActionLink() in process and change the output. Then I can use all the standard calls.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么这个有“臭味”? Microsoft 将 ActionLink 方法构建为扩展方法。 如果这些不能满足您的目的,您可以像以前一样编写自己的。
如果您确实想要,我想您可以创建您自己的 ActionLink 方法版本作为重载(只要方法签名不模糊)。
如果您需要了解 MS 如何实现 ActionLink 扩展方法,请查看 LinkExtensions 类下的源代码。 您可以在此处下载。
Why does this have a "smell"? Microsoft built the ActionLink methods as extension methods. If these do not serve your purposes, you can write your own just as you have done.
If you really wanted, I guess you could create your own version of the ActionLink method as an overload (as long as the method signatures aren't ambiguous).
If you need to see how MS implemented the ActionLink extension method, check out the source code under the LinkExtensions class. You can download it here.