3

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://.

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

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?

Iliya Krinchiyan
  • 267
  • 5
  • 14
  • When you load the page with the redirect, does the system you're testing on have an app which states that it handles your custom scheme? (how exactly are you testing) – Wain Jan 16 '14 at 12:07
  • its simple executing window.location.href = 'myschema://someaddress.com'; in js(javascript) will cause ios to launch app. – 2intor Jan 16 '14 at 12:11
  • @Wain - I am expecting that redirect will change url in browser to my custom url and show browser's error message "can't open this page..." I am testing on windows pc. – Iliya Krinchiyan Jan 16 '14 at 12:38
  • @2intor Thank you. I've started testing this way now, but this involves creating simple html page and I was thinking that it is possible to do redirection straight from the server side without returning html and using javascrip – Iliya Krinchiyan Jan 16 '14 at 12:39
  • No, it definitely has to be a redirect handled at the client end. – Wain Jan 16 '14 at 14:05
  • what happens exactly after the redirect?any error? – techBeginner Jan 16 '14 at 14:55

2 Answers2

1

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.

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

    }
}

On Fiddler this now does:

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
Dave Walker
  • 3,498
  • 1
  • 24
  • 25
  • Thank you for this answer. I didn't try it yet and I am using client redirection now, but is sounds interesting so I'll mark it as an accepted answer. – Iliya Krinchiyan Feb 12 '14 at 21:24
1

I had the same kind of question this very morning. I would set a string like:

string myUrl = "mySchema:parameters";

and upon calling, in an Action:

return Redirect(myUrl);

I would not see the redirect happening.

I tried to create a custom class inheriting from RedirectResult (note that RedirectResult is a class available in System.Net.Mvc), similarly to what is presented in @DaveWalker's answer, but that made no difference to me.

After banging my head for a couple of hours I decided to test the call on an actual mobile device: lo and behold, the redirect happens correctly there.

This has probably something to do with how pc browsers are implemented compared with those on mobile devices.

So, to summarize, even though it cannot be seen on a PC browser, doing

return Redirect(urlWithCustomSchema);

works fine on mobile devices.

Bovaz
  • 375
  • 6
  • 20