 |
| General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category.
** PLEASE BE SPECIFIC WITH YOUR QUESTION **
When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 21st, 2006, 11:20 PM
|
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Redirect within a class
Hi all,
I am working on an ASP.NET project and was wondering if there was a way to use the redirect method in a class outside of the code behind file? The same goes for reading/writing cookies...
The following is a pseudo-walkthrough of what im doing (hopefully to get my question understood better):
I have a login page, default.aspx. The user clicks on the 'Login' button after entering their login and password information.
The button click event resides in the code-behind file. The event creates a UserAction object from another class (which i call the business layer class) which contains the validation rules. I want to be able to read/write cookies, and redirect the user from this class, and not -have- to do it from the code-behind or the "UI" portion of the application.
I've tried looking around MSDN and have found classes such as HttpResponse, HttpRequest, etc but I guess I dont understand how to use these correctly. I have been able to get to what I believe was close, but the problem there was that it required a "prefix" of which I understand being the parts of a url. After selecting the url of my web application, I get an error stating that the connection is in use.
I apologize for rambling so much ;p
Thanks in advance,
Rolandatem
|
|

January 22nd, 2006, 07:51 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Rolandatem,
You can get access to all the intrinsic ASP.NET objects like Server, Response etc through the HttpContext.Current object which represents the "current" HTTP environment.
For example, you can use the following code to change the value of a cookie from a Class Library project:
System.Web.HttpContext.Current.Response.Cookies["SomeCookie"].Value = "SomeValue";
You may need to add a reference to the System.Web DLL.
Note that using HttpContext.Current ties your business layer to a web application; there is no way to reuse it anymore in, say, a Windows Forms application, more or less breaking the rules for a Class Library.
Also, personally, I would never redirect a user from within a business layer class. Reading from and writing to cookies or sing session variables seems acceptable, but redirecting is confusing. Future developers on your web project probably have no idea that
myCustomer.CheckOrderStatus()
might redirect them away to another page. It's probably a lot cleaner to let the Code Behind handle the redirect, while the business checks the rules for redirecting. E.g. it returns some kind of status or other information, that the Code Behind uses to determine what page to redirect to.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

January 22nd, 2006, 03:32 PM
|
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks so much for that, it makes more sense now. While trying to figure this out I was wondering how the class created would relate to the page currently in use, so now it make more sense.
Also, I see what you're saying with the redirect issue. At first I figured that would be a business rule, but then after rethinking the issue, if I swapped out the UI to say a windows form, it would break the whole tier structure and would have made it pointless to have the code seperated.
So let me get this straight. My component is just a login validator. It would make more sense the have my login button click event use the business layers validate method and return an integer (which would be the user's db id), and have the code-behind to set the user_id cookie and redirect the user?
|
|

January 23rd, 2006, 07:39 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Yeah, IMO, redirecting is a presentation thing. In an ASP.NET app, you redirect to another page. In a Windows Forms app, a "redirect" might be a message box or an entirely new form being presented.
There are a few ways to tackle this, depending on your needs. You could, for example, have a Login() method that returns True or False depending on criteria you specify (usually a user name and a password). Then if Login() returns True, you can access the database again and get information for the requested user.
Alternatively, your Login method could return a Customer object (just an example). When the object equals null, login failed. Otherwise, you can use the Customer object for other purposes (get its Id, name, whatever).
E.g.:
Dim myCustomer As Customer = Customer.Login(userName, password)
If myCustomer Is Nothing Then
' Login failed
Response.Redirect("NoWay.aspx")
Else
' Login succeeded.
End If
It's a matter of preference. Personally, I would create a separate Login method that returns a Boolean. Then on login success, I'd get the associated Customer record.
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|
 |