In one of my ASP.NET MVC application I'm using an actionlink to switch the language, e.g. http://example.com/nl/home/switch?lang=en&redirect=/en/home/about. When the url is pressed, the switch function redirects the user to /en/home/about with language en (English). The problem is Google is indexing the switch actionlink as if it is the linke-to page. So in this example /en/home/about shows up in the Google results as http://example.com/nl/home/switch?lang=en&redirect=/en/home/about instead of http://example.com/en/home/about. I think rel="nofollow" is not an option since the linked-to page will be ignored. Also, in the actionresult I'm using the function Redirect(..);, correct me if I'm wrong, but doesn't the Redirect function sends header 302? Any suggestions to solve this?
Google doesn't deal well with 302 redirects. A 302 redirect is a "temporary" redirect. When you issue a 302, Google may prefer to index the redirect itself because you are saying that the item that it is pointing to may change. If you changed your redirect code to issue a 301 permanent redirect, I believe that Google would behave as you desire.
Related
What is the difference between Redirect and RedirectToAction other than their return type?
When do we use each? Explanation with any real life scenario would help me greatly.
I was looking at Confusion between Redirect and RedirectToAction, but, to me, it looks like the answer is more specific towards handling id parameter and returning proper view.
RedirectToAction lets you construct a redirect url to a specific action/controller in your application, that is, it'll use the route table to generate the correct URL.
Redirect requires that you provide a full URL to redirect to.
If you have an action Index on controller Home with parameter Id:
You can use RedirectToAction("Index", "Home", new { id = 5 }) which will generate the URL for you based on your route table.
You can use Redirect but must construct the URL yourself, so you pass Redirect("/Home/Index/5") or however your route table works.
You can't redirect to google.com (an external URL) using RedirectToAction, you must use Redirect.
RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table.
Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.
Best Practices: Use RedirectToAction for anything dealing with your application actions/controllers. If you use Redirect and provide the URL, you'll need to modify those URLs explicitly when your route table changes.
I'm doing a RedirectToAction like this:
return RedirectToAction("index", "mycontroller", new RouteValueDictionary(
new {
a = 1
}
));
But when I'm redirected, the URL has some odd characters at the end, namely #_#_ so it looks like this:
http://mysite.com/?a=1#_#_
I'm a little confused how those characters are getting there, seeing as I'm not appending them. Any ideas?
Is the browser being redirected to your action from Facebook? Facebook is at least adding this kind fragments to redirect URLs for security reasons. And browsers seem to be doing little magic with these fragments:
After much debugging, I realized that Firefox, Chrome, and Opera will re-attach a URL Fragment after a HTTP/3xx redirection has taken place, even though that fragment was not present in the URL specified by the Location header on the redirection response.
Source: http://blogs.msdn.com/b/ieinternals/archive/2011/05/17/url-fragments-and-redirects-anchor-hash-missing.aspx
This has nothing to do with ASP.NET MVC. You probably have some client side javascript or plugin which does this and appends a fragment to the url. Or maybe some plugin on your browser. Note that you can append fragments to the url (everything that follows the # sign) using javascript without the browser redirecting.
Okay we have a single - sign - on and the user will likely enter www.blabla.com/AppName/ to reach our site. We then define a welcome site, use a phaselistener to check:
is user trying to access the welcome site? yes -> try to login - works? yes -> get user roles -> forward to the appropriate site for this specific user.
E.g. user niceBelly goes to page /somewhere/in/many/folders/beer.jsf and user barbie goes to /breasts/pink.jsf
a redirect is in this application not possible for some reasons.
the result is that when reaching e.g. page pink.jsf the address bar still shows blablaba.com/AppName/
clicking the first link will result in the browser using the form address as new URL e.g. on welcome.jsf i navigate to coolstuff.jsf. On the page coolstuff i now have the url of the last form, e.g. welcome.jsf. Then on cool stuff i click a link, and get coolstuff on the next page as url, and so on.
Is there a way to solve this / work around it?
Given the symptoms, you are actually not redirecting the requests, but you are actually forwarding the requests. A real redirect will take place when you call
externalContext.redirect(url);
in JSF context, or when you add
<redirect />
to the navigation case. All other ways are effectively forwards. As per the symptoms, you're using commandlinks instead of outputlinks to navigate to other page. Commandlinks will submit a POST request to current URL and JSF will under the covers use RequestDispatcher to set the destination of the request/response when the navigation case doesn't contain <redirect />. A forward does not instruct the browser to fire a new GET request on the destination URL and hence the URL in browser address bar does not change. A real redirect will do exactly that.
See also:
When should I use outputlink instead of commandlink?
Bookmarkable URLs in JSF 1.x
I'm trying to perform a redirect from one controller to another while passing along some parameters. Unfortunately the value of the parameters are long and obnoxious looking in the GET query string. Is there anyway I can use POST instead on the redirect to get a nicer looking URL?
Thanks.
As everyone said, you cannot redirect to post.
However, you can avoid ugly URLs by sticking your values in TempData instead of the route.
You can't do a proper POST redirect, but you can use JavaScript in the browser to mimic a POST redirect:
<form id="myform" action="http://mywebsite.com/">
<input name="myparameter" value="12345" type="hidden" />
</form>
<script>
document.getElementById("myform").submit()
</script>
It's not a true redirect and it won't perform as well as a redirect, but this should work.
A "Redirect" is a GET. If you're doing a real direct what you're doing is essentially informing the browser to go to another url. The browser than makes another http call (using GET) to the new url. The url may contain parameters but it will always be a GET and not a POST.
What you could do is store some data in session and then when the second (redirected) request comes in, you can access these values from session.
Sorry, no. Redirects via POST are simply not supported in the HTTP spec. Most clients implementing the common kinds of redirects (301 - Permanent and 302 - Temporary) issue a GET request to the new location.
Here is my use case
I have a search model with two actions search_set and search_show.
1 - A user loads the home page which contains a search_form, rendered
via a partial (search_form).
2 - User does a search, and the request goes to search_set, the search
is saved and a redirect happens to search_show page which again
renders the search_form with the saved search preferences. This search
form is different than the one if step1, because it's a remote form
being submitted to the same action (search set)
3 - Now the user does another search, and the search form is submitted
via ajax to the search_set action. The search is saved and executed
and now I need to present the result via rjs templates (corresponding
to search_show). I am told that if the request is xhr then I can't
redirect to the search_show action? Is that right? If yes, how do I
handle this?
Here is my controller class
http://pastie.org/993460
Thanks
That's right. Either make the request non-XHR and redirect as normal, or you could try rendering the URL you want to redirect to as text or part of a JSON object which your AJAX request then uses to call document.location.href = [whatever] (but this seems hacky).
Right now what's happening is your XHR request is returning the result of the redirect, and not actually redirecting the page that made the XHR request.