重定向到具有自定义架构外部URL [英] Redirect to external URL with custom schema

查看:147
本文介绍了重定向到具有自定义架构外部URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Asp.net MVC4项目。我要让重定向到外部URL如果指定的操作就会被调用。
URL应该有自定义模式,例如没有的http:// ,但 MYSCHEMA://

I have Asp.net MVC4 project. I want to make redirect to the external url if specified action is called. Url should have custom schema, for example not http:// but myschema://.

我知道重定向到google.com我可以使用返回重定向(http://google.com),但这并不如我的工作叫返回重定向(MYSCHEMA://someaddress.com)

I know that to redirect to google.com I can use return Redirect("http://google.com"), but this doesn't work if I call return Redirect("myschema://someaddress.com")

我需要这种定制模式开始IOS设备上的应用程序,我需要做的重定向MVC中的项目,因为我想发送链接的电子邮件,该链接会导致我的网站上行动,这一行动将不重定向到自定义架构

I need this custom schema to start app on IOS device and I need to do redirection in MVC project because I want to send link to email, this link will lead to action on my site and this action will do redirect to custom schema.

直接邮件发送自定义模式链接不工作,因为邮件服务器删除邮件此链接。
此外,我不希望用户重定向到前端,在那里他将需要点击与自定义架构的链接。

Sending link with custom schema directly in mail is not working because mail servers delete this link from the mail. Also I don't want to redirect user to the frontend where he will need to click to the link with custom schema.

是否有可能或者我应该做它在其他的方式?

Is it possible or should I do it in other way?

推荐答案

您可以创建自定义RedirectResult。如果你挖通过asp.net的MVC RedirectResult源$ C ​​$ C,可制定出pretty很快它在做什么。

you can create a custom RedirectResult. If you dig through the source code of the asp.net mvc RedirectResult you can work out pretty quickly what it's doing.

这里有一个我撞了pretty快。

Here's one I banged out pretty quick.

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);

    }
}

在此提琴手现在这样:

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

这篇关于重定向到具有自定义架构外部URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆