Inline Redirect in MVC 5 - asp.net-mvc

We're in the process of updating an MVC application. Currently, we cannot change any of the backend code and need to do an inline redirect on the razor page. How can I do a redirect to a URL directly in a razor page?

One possibility is to use the Redirect method:
#{
Response.Redirect("http://google.com");
}
or if you wanted to redirect to a controller action in the same application:
#{
Response.Redirect(Url.Action("SomeAction", "SomeController"));
}
This will perform the redirect on the server by returning the 302 status code. If you want to make the redirect on the client you could either use the meta tag in the head section of the DOM:
<meta http-equiv="refresh" content="0;URL=http://google.com">
or plain javascript:
<script>
window.location = 'http://google.com';
</script>

Related

Redirecting page from Thymeleaf template

How to redirect the page from Thymeleaf (I have JSP code like below)
out.println("REDIRECT=http://www.example.com/api/response?id="+id)
What is the equivalent in Thymeleaf? I want to do it from the template.
Thymeleaf doesn't provide any mechanism for redirecting to another page from one of its templates -- and I wouldn't recommend doing it anyways (since this is something that should probably be handled at the controller level).
That being said, it's possible to do this using javascript. Something like this:
<script th:inline="javascript">
window.location.href = 'http://www.example.com/api/response?id=' + [[${id}]];
</script>
Or possibly the meta refresh tag.
<meta http-equiv="refresh" th:attr="content=${'5; url=http://example.com/api/response?id=' + id}" />

Redirect from an MVC view

Does anyone know the syntax for a redirect from a view to another view?
I mean, I need to add the redirect code in the actual view, not the controller.
You cannot do this directly in the view with MVC code. The view handles the display of a page only, and is only reached in the code once the final destination of a request has been determined. You can do this however with both JavaScript and HTML:
JavaScript:
window.location.href = "http://example.com";
HTML:
<meta http-equiv="refresh" content="0;URL='http://example.com/'">
This method is discouraged by the W3C and should not be used. I have only included it here for informational purposes.
All that said, you should never have to do this. If you are trying to implement this in your MVC site then you are not respecting the MVC pattern and should rethink your architecture.
To automatically redirect, you can use
Javascript:
window.location.href="otherView";
Meta Refresh
<META HTTP-EQUIV="refresh" CONTENT="N;URL=http://www.foo.com/YourView">
I would do this via javascript
$(function(){
window.location.href = "www.yournewUrl.com";
});
or
$(function(){
window.location.href = "#Html.ActionLink("linkie, "action", "controller")"
});
You can use this
<meta http-equiv="refresh" content="10; ,URL=http://whatev.com/dd.html">
but it is not recommended

JSF wrong redirection when catching ViewExpiredException

So I have a ViewExpiredException Handler and works fine.
Now, when I launch the web-app my URL looks like www.myApp.com/TestFaces/ and this presents the first page which is the login page.
If for any reason I leave the page at login, and the View expires the app catches the ViewExpiredException and sends me to a page "ViewExpired" BUT the URL keeps the same www.myApp.com/TestFaces/. On that "ViewExpired" page I have a commandLink to return to the login page which in the value attrib I put "index.xhtml" BUT it does not send me to login page because on there's no page on www.myApp.com/TestFaces/index.xhtml but in www.myApp.com/TestFaces/faces/index.xhtml
So the questions are:
Why if I'm at login page am I getting the ViewExpiredException? is it because of ajax?
How or Where can I make the commandLink really sends me to index.xhtml?
This is only happening when the View expires in login page, in other pages from my app it works really great.
Thanks in advance !
Why if I'm at login page am I getting the ViewExpiredException? is it because of ajax?
You will get this exception when you invoke a POST request on a view which does not exist in session anymore. This can for example happen when you keep the page open for too long that the session has expired in the server side, or when you're getting the login page from the browser cache instead of straight from the server. For more detail, see also our ViewExpiredException tag info page. All JSF ajax requests also accounts in this as they also use POST.
How or Where can I make the commandLink really sends me to index.xhtml?
Make use of implicit navigation. This way JSF will append the proper FacesServlet mapping.
public String goToIndexPage() {
return "index";
}
or
<h:commandLink value="Go to index page" action="index" />
or, better, when you don't need to invoke any business logic at all:
<h:link value="Go to index page" outcome="index" />
See also:
Check if session exists JSF
When should I use h:outputLink instead of h:commandLink?

jquery ajax calls in an mvc site not sharing logged in session

as in the title
we're firing
$('#id').load('someUrl', function(){/*some stuff*/});
But it seems to be being passed to the login form because it's not logged in?!
The user is logged in. Is this normal?
This happened to me too, but the cause was not that ajax is going crazy - which it may look like - but that the URL was not correct.
The website was initially hosted on localhost, without any alias, so I had a working URL like localhost/controller/action.
Then I moved the website under an alias - so the website's URL was localhost/alias. At this point, two strange things happaned to SOME ajax requests:
- the requests were prompted for authentication (the website was using windows authentication)
- the requests' session was different than the one of the page which launched the request
The problem was that some URLs were hardcoded in the js code. So we had code like "url: '/controller/action'" in the ajax calls code, and this URL didn't take into consideration the alias under which the website was hosted in IIS.
The solution was to declare a js variable in the page and assign it the URL built the right way, with Url.Action, and then use the variable in the ajax call:
Page:
<script type="text/javascript">
var ajaxUrl = '#Url.Action("actionname", "controllername")';
</script>
Js file:
$.ajax({
type: 'POST',
url: ajaxUrl,
...

Retain Url Fragment within ReturnUrl in Forms Authentation under MVC

I have a scenario where I need to send the user to the login page, with the returnurl parameter populated with the page they're currently sitting on including a url fragment, so as when they complete login, they're redirected back to their original page and the page scrolls down to a specific #location.
At present, it's all working except that the url fragment is lost when the returnUrl param reaches the Login ActionMethod.
Is there a way to retain this url fragment so it doesn't get lost during the login phase? I can see the #fragment in the url on the login page, but it appears to be stripped off when I look at the value of 'returnUrl' in my login method.
Is there a way to retain this url fragment so it doesn't get lost during the login phase?
No, there isn't. The url fragment is never sent to the server. One possibility is to modify the returnUrl on the client before passing it to the server so that the url fragment becomes part of the query string. Then when the login succeeds and the server needs to redirect back to the returnUrl it would modify it to the original value.
For example it might look like this before sending it to the Login method:
http://example.com/admin/index?fragment=somefragment
If your redirected login url looks like this https://example.com/Login?ReturnUrl=%2F#/protected/page, anything to the right of the hash(#) never gets passed to the server. If you inspect your form, you will see the form action omits the hash fragment.
<form action="/Login?ReturnUrl=%2F" id="login-form" method="post" role="form">
....
</form>
Notice the missing fragment in form action. The way I solve this is by capturing the hash fragment from the url and adding it back to the form action through javascript. Right below the login form, I have this inline script:
<script type="text/javascript">
window.onload = function () {
var hash = window.location.hash.substr(1);
if (hash !== "") {
var form = document.getElementById('login-form');
var action = form.getAttribute("action") + 'NHASH' + encodeURIComponent(hash);
form.setAttribute("action", action);
}
}
</script>
So now the form action will look like this action="/Login?ReturnUrl=%2FNHASH%2Fprotected%2Fpage". On the server side, after successful authentication and before redirecting to the ReturnUrl, you replace the NHASH with # such as
returnUrl = returnUrl.Replace("NHASH", "#");
Note: You can use any token for #. I just chose NHASH

Resources