I have this:
public RedirectResult LinkRedirect(string url)
{
return Redirect(url);
}
And all it does, is redirecting me to http://mysite.com/www.externalsite.com. What am I missing here?
You need to pass a url with http://prefixed to it. Then it will work
Ex: http://www.google.com
So if you want to allow your action method to accept all kind of links( with and without http prefix), you need to write little bit of code to check whether the passed the url parameter value has http prefix and if not, append it.
Related
I've built a spring-web-mvc service which delivers some basic pages using Thymeleaf. Now I've placed a button on one of the sites which should redirect the user (back) to an app by using the app deep link. The deep link schema looks like following: 'my.app://'
When pressing the button the underlying controller is invoked returning a string
#PostMapping("getBackToApp")
public String getBackToApp(#RequestParam final String myCoolAppLink) {
return "redirect:" + myCoolAppLink
}
The controller gets invoked and returns the corresponding string.
I have thought that the user would get redirected "mycoolAppLink" (should be shown in the URL bar of the corresponding browser) but instead the "myCoolAppLink" gets attached to the current URL which for example looks like following:
http://localhost:9999/myApp/myPage/my.app://
instead of
my.app://
So it seems like that when the protocol is missing the redirect instruction gets appended to the current URL.
I also tried it using a GET controller, or performing a redirect instruction using the HttpServletRequest but neither of the approaches seem to work.
As the deep link of apps do not have a correct protocol (http / https) I wonder how to achieve this? Maybe I am getting something wrong but I thought sending a redirect back to the client with the specific URL should do the trick!
PS: I not tried it on a mobile device but I assume that it wouldn't work.
Thanks!
I believe you can use the RedirectView
#RequestMapping("/getBackToApp")
public RedirectView localRedirect() {
RedirectView redirectView = new RedirectView();
redirectView.setUrl("my.app://");
return redirectView;
}
Although I have yet to try this with a "none" http url
I am trying to send a string with some special characters to my Asp.Net Web Api controller. However, Asp.Net can't seem to resolve the url encoded string. Sending something like "A%2F223%2F4" is the encoding for "A/223/4" and the same also doesn't work for backward slashes. It does work for other special characters though. Is there any way to get this working? Or is it possible to turn the automatic decoding off, so that I can do it manually?
This is my functions inside my controller:
[HttpGet, Route("getByArtNr/{articleNr}")]
public IHttpActionResult GetByArtNr(string articleNr)
{
Article article = dbContext.Article.Where(x => x.ArtNr == articleNr).FirstOrDefault();
if(article == null)
return NotFound();
return Ok(article);
}
Example request:
http://localhost:54282/api/v1/article/getByArtNr/A%2F223%2F4/
In order to send a string which has / (slashes) in it, we can use the wildcard parameters in the route. Something like this:
Route("getByArtNr/{*articleNr}")
This will allow for a chunk of a URL with multiple / characters to be read as a single parameter.
Hope it helps.
When I do a redirectToRoute, I want to pass hidden parameters.
Pass the parameter in a route does not satisfating me, because I want the information hidden.
I was thinking to do that with POST, but I don't know how to pass POST parameters in the redirectToRoute function.
Can I do that ?
You can't redirect a POST request because the browser would have to resend the POST data , so you have to use forward like:
return $this->redirectToRoute('name_of_route_to_redirect', ['max' => 10,...]);
For more information , take a see in the documentation symfony controller
You can send the Request object inside :
return $this->redirectToRoute('route', [
'request' => $request
], 307);
that 307 guarantees that the method and the body will not be changed
when the redirected request is made
What you could do is storing your parameters in some file/memory cache.
You redirect without parameters and on your action you check if you have some parameters in the file/memory cache and get them from there.
I don't know if it's a good thing to do though, but it seems possible to do.
Here is the thing:
Let's say I have a base URL like this:
www.mysite.com?my_first_param=1&my_second_param=2
But now I want to add a new parameter to my URL:
www.mysite.com?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com
Imagine that I am using a service where they add new tracking parameters to my URL. For example:
first_tracking=value1&second_value=value2
Since my BASE url already has a "?" the parameters are added with a "&", so my final URL would look like this:
www.mysite.com?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com&first_tracking=value1&second_value=value2
This is a correct URL with several parameters, but when I do the redirect to www.myothersite.com, since the parameters start with a "&", they get lost. Would it be correct to add the tracking parameters with a starting "?" ? Like this:
www.mysite.com?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com?first_tracking=value1&second_value=value2
If not, what would be a good approach for dealing with this? I believe is responsibility of the redirect to pass through the tracking parameters to the redirect URL.
You should URLencode each param name and value properly, so if the map of params you want is this:
my_first_param => 1
my_second_param => 2
my_redirect_site => www.myothersite.com?first_tracking=value1&second_value=value2
then you should pass this as the query string:
?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com%3Ffirst_tracking%3Dvalue1%26second_value%3Dvalue2
You should be using a library which does this already to build up URIs to handle this encoding for you.
No, you can't have a second question mark in a URL.
Furthermore, if you have ampersands in the redirect URL, they will be seen as separate parameters for the main URL, and not seen as connected to the redirect URL.
If you want to do a redirect like this, you need to URLEncode the whole of the redirected URL. There are standard functions in most web-facing languages to do this.
Encode the parameter, "?" would be replaced by %3F
It depends on the amount of control you have over the service adding the tracking parameters.
Can you change the url after the parameters have been added?
If you can, then you should use a url builder to add the tracking parameters to your redirect url, then url encode that url entriely, tracking parameters included.
If you are not in control and a third party modifies your url, then you will have to do this when you redirect, read the parameters in the url, take your redirect url and the tracking parameters, add the tracking parameters to the redirect url before redirect.
i want to make some questions about asp.net mvc.Actually,i am not familiar with web developing.my questions are i have two controllers,one is login and another one is
profile.After login,i want to call profile.so,i use this way,
return RedirectToAction("DiaplayProfile","Profile",new { personID = Person.personID});
my problem is parameter that i passed are shown i URL.i don't want to show.In web developing,if we use post method to send data,parameters are not shown in url,is it correct?for me,i need to pass parameter ,but,i don't want to show at url.
How i use in post method in MVC RedirectToAction?i also tested with TempData
to pass data,but,it's gone after if i make refresh.how i hanlde this?And also,
can i use [AcceptVerbs(HttpVerbs.Post)] for solve this problem?
guide me right ways, please.
Regards
Chong
You can't do a post as part of a redirect; a redirect must be a get request since you're simply passing the browser the URL to which to redirect. In MVC, having the id as part of the URL is actually considered to be a good thing. I would embrace it. Rather than having the parameter be personID, I would use id so that the default routing can be used. Eventually I'd want to end up with a URL that looks like:
http://example.com/profile/display/1345
where 1345 is the id of the person in question. If you use default routing, that would mean that you have a ProfileController with a Display method that takes a single id parameter (of type int). You would redirect to it using:
return RedirectToAction( "display", "profile", new { id = 1345 } );
BTW, you can't use the AcceptVerbs attribute to control how a redirect is made, only to control what HTTP verbs the action will respond to. In this case, I think you really do want to accept a GET and I think you really do want to have the id in the URL.