Link to home
Start Free TrialLog in
Avatar of Moti Mashiah
Moti MashiahFlag for Canada

asked on

asp.net

Hi Guys,
I have web application that run asp.net MVC 4, but my login page is login.aspx, when I login.. it directs me to another page which is "data.aspx".
Now I built MVC controller with view and I let users manually to type this URL and pass param to my controller and I return view... like kinda of API.
For example: http://localhost/webapi/gettable/param1/param2.

When I loggedin the URL works perfectly, but when I logout and try it requires me to login which is fine, but direct me to the data.aspx.

I'm wondering how can I redirect users to my MVC url after login instead to the source return in a case user type this URL.

Thank you.
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

but my login page is login.aspx, when I login.. it directs me to another page which is "data.aspx".
you can simply redirect to your MVC mapped route URL directly.

how you do redirection to data.aspx?

can you post the codes in your RouteConfig.cs?
Avatar of Moti Mashiah

ASKER

yep, Here is my code for routing which goes to my Signin.aspx login page .

routes.MapPageRoute("WebFormRoute1", "Account/Login", "~/SignIn.aspx", False, Nothing, New RouteValueDictionary(New With { _
                Key .controller = New IncomingRequestConstraint() _
        }))

Open in new window


Then, it goes to the method inside the signing.aspx code behind and direct to data.aspx which is what should happen.

Now I have my controller and view that act like API let's say user didn't login yet and he type this URL http://localhost/webapi/gettable/param1/param2.. which receive 3 params, so the user will send to the login page, then login and get the data.aspx.

my question is - how do I give condition in the login page by saying if user come from this controller then return this url http://localhost/webapi/gettable/param1/param2 include params.
Btw, this is my view for the controller which I would like to redirect user when they come from it.

 routes.MapRoute( _
         name:="viewr_for_grid", _
         url:="{controller}/{action}/{view1}/{rowid}/{view2}", _
         defaults:=New With {.controller = "WebApi", .action = "GetTable", .view1 = UrlParameter.Optional,.rowid = UrlParameter.Optional, .view2 = UrlParameter.Optional} _
     )

Open in new window

I just want to clarify again. I would like to return this customize URL just if user access to this url.


Thanks.
I think in your Controller : WebApi, you can refer to Request.UrlReferrer to get the page before it's redirected.
and then save this into a session or viewbag so that it can be referred later.
I was trying to do something like that to put my url in session and refer later after the login, but still when the login happens I need to put condition in the login Method and say there is request from the webapi so redirect to this url.

The problem is that I cant see this session in the login method.
The problem is that I cant see this session in the login method.
most likely you can...

how's your codes look like in your login Method?
Sorry, I can't expose my login method for security purposes, but in a big picture my method just return data.aspx.

If you can send me some code sample of what I need to write in this method to refer the previous link.

Let's say user point to this link -  http://localhost/webapi/gettable/param1/param2  before login, so the first step they hit the gettable method.

In this method I keep the URL in session like:
Session("webapi") = Request.UrlReferrer

then, in the login method I would do:
If Not Session("webapi") Is Nothing Then
                   
  End If

The issue with what I'm trying to do is I don't get the session in the login method.
I got no idea where you put the code of:

Session("webapi") = Request.UrlReferrer

Open in new window


can you provide more details? is it in your Controller? how you actually put the code in the Controller?
yes, this code is in my controller:
This is the method I have in my controller:

Here is part of the code (can't show the full code for security purposes.

 Public Function GetTable(view1 As String, rowid? As Integer, view2 As String) As ActionResult
        Session("webapi") = Request.UrlReferrer 

End Function

Open in new window

Let's say user go to this method in my controller without logIn, so it will direct them to the login page. Now when they login it will take them to the login method and there I'm going to put a condition.

If Not Session("webapi") Is Nothing Then
                    
  End If

Open in new window

then return this URL.

Let me know if is it clear enough.
well, I shall provide some codes for you when I go back office tomorrow.
k, thank you sooo much for helping me.

btw, it seems that when I store veritable in my session through MVC it doesn't work in the login method in the aspx page. I'm wondering if there is other option to store the URL variable.
session should be compatible with MVC as well.
Then I have  o idea why the variable is not passing to the login method by providing the session.
think in your ActionResult you can simply check session like this: (sorry that codes are in C#)

public ActionResult Index()
        {
            if (Session["UserID"] == null)
            {
                Session["RedirectURL"] = Request.Url.LocalPath;
                return RedirectToAction("Login");
            }
            return View();
        }

Open in new window


so use Session["UserID"] to check login status and use Session["RedirectURL"] for redirection.

hope this make sense.
That is make sense, and this is exactly what I do, but after this method runs it goes to the login page and request login.

Now I login and it goes to the login method which is in my  login.aspx code behind page.

When I enter to this method I give condition....like:

"on depend on your method"
if (Session["RedirectURL"]  != null){
 var URL = Session["RedirectURL"]
 redirect(URL)
}

The issue I have here is that the session come with no value
but after this method runs it goes to the login page and request login.
what does this mean?

in your login page, why you need to redirect to another page?
Hi Ryan,
I figured out that I just need to know how to pass session or cookie from mvc controller method to aspx page.

Do you know about any way to do you.
I just need to know how to pass session or cookie from mvc controller method to aspx page.

In my aspx, I can just read the session if it's being set.

protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(Session["RedirectURL"].ToString());
        }

Open in new window

This code gives me an exception.
it gives you an exception because the session object is not initiated, technically you should able to access the session variables
Van you tell how.
Dim test = Response.Write(Session["RedirectURL"].ToString())

I can't do something like this.

Please, can you tell what do you mean and how to do it.

Thank you.
>> Dim test = Response.Write(Session["RedirectURL"].ToString())

?

it should be:

Dim test = Session["RedirectURL"].ToString()

Open in new window

Basically that how I started my question :).

This not working as you can't send session from mvc to aspx as it will come null.
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Again my issue is to pass session from MVC controller to aspx.

I really appreciate your help.
As we can't pass session to aspx page from the controller.

I have thought maybe I can pass the param to the global.asax like:

in the session_start. method
 Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
 
       session("Myvariable") = HttpContext.Current.Request.Url.AbsoluteUri

           End Sub

Open in new window


basically I found this helpful as if you assign the variable here you can see it in the SignIn.aspx page.
The only issue I have is that the URL comes like SignIn.aspx instead the one from mvc as first I need to run this "HttpContext.Current.Request.Url.AbsoluteUri" in
MVC method than pass it to the global.

My question now is. How can I pass variable between my method in the controller to the global.asax.

let's say this is my method in my controller:
Sub MethodMVC()
session("test") = HttpContext.Current.Request.Url.AbsoluteUri
end sub

Open in new window


Now I have session with my URL and I'm going to send this session to global.

 Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
 
       session("Myvariable") =   Session("test")

           End Sub

Open in new window


I know its not working, but I was trying to give an idea.

can you tell how can I do something like this.
Hi Ryan,
First of all I have to Admit you were all the time right session can pass between MVC and aspx pages.

Now I can tell you what was my issue :).

The issue was in the logout method :

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Session.Abandon()
        Session("Passport") = Nothing
        Response.Cookies("Islogin").Value = False
        
        Response.Redirect("signin.aspx?out=1")
    End Sub

Open in new window

When I used to send the URL before the login it was going to the logout method which have the "session.abandon()" that clear all the session.
so I took off the session.abandon() and clear just the passport which was the only session with the critical values.
:) thank you for help.
Hi Ryan,
First of all I have to Admit you were all the time right session can pass between MVC and aspx pages.

Now I can tell you what was my issue :).

The issue was in the logout method :

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Session.Abandon()
        Session("Passport") = Nothing
        Response.Cookies("Islogin").Value = False
       
        Response.Redirect("signin.aspx?out=1")
    End Sub

When I used to send the URL before the login it was going to the logout method which have the "session.abandon()" that clear all the session.
so I took off the session.abandon() and clear just the passport which was the only session with the critical values.
:) thank you for help.
glad that you found the root cause of the issue