我有 Asp.net MVC4 项目。如果调用指定的操作,我想重定向到外部 url。
URL 应该有自定义架构,例如没有http://
but myschema://
.
我知道要重定向到 google.com 我可以使用return Redirect("http://google.com")
,但是如果我打电话,这不起作用return Redirect("myschema://someaddress.com")
我需要这个自定义架构来在 IOS 设备上启动应用程序,并且我需要在 MVC 项目中进行重定向,因为我想发送电子邮件链接,此链接将导致我的网站上的操作,并且此操作将重定向到自定义架构。
直接在邮件中发送具有自定义架构的链接不起作用,因为邮件服务器从邮件中删除了此链接。
另外,我不想将用户重定向到前端,他需要单击具有自定义架构的链接。
是否有可能或者我应该以其他方式做到这一点?
您可以创建自定义 RedirectResult。如果您深入研究 asp.net mvc RedirectResult 的源代码,您可以很快地弄清楚它在做什么。
这是我很快就敲出来的。
public class RedirectResult : ActionResult
{
private string _url;
public RedirectResult(string url)
{
_url = url;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
string url = UrlHelper.GenerateContentUrl(_url, context.HttpContext);
context.Controller.TempData.Keep();
HttpResponseBase response = context.HttpContext.Response;
response.RedirectPermanent(url, endResponse: true);
}
}
在 Fiddler 上,现在这样做:
HTTP/1.1 301 Moved Permanently
Date: Tue, 11 Feb 2014 14:03:39 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 3.0
Location: myschema://someaddress.com
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 149
Connection: Close
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)