In a standard MVC 3 project in VS 2010, what can normal class files, eg Class1.cs1, use to make use of the MVC pattern? The files I am talking about are not the controller or view or model. They are doing some common tasks, but they still need to interact
with the rest of the application. In these files, how do I use routes or call the controller actions? Do I use httpContext only and code in the old web form way?
They are doing some common tasks, but they still need to interact with the rest of the application. In these files, how do I use routes or call the controller actions?
For example, in one of my methods in Class1.cs I need to do a HttpContext.Current.Response.Redirect ("...."). Should I use the Redirect method? Or is there some way I can call a controller action from this piece of code?
HttpContext.Current.Response.Redirect ("....")......need just to agg the namespace the HttpContext is contained in...and calling HttpContext.Current.Response.Redirect ("...."),,,,,,you can do it.
However it is not good practice to call Web Environment function in Business classes
I forgot to mention that the execution of my code does not originate from a controller action. It is from an overriden method (FormsAuthentication_OnAuthenticate) located in Global.asax.cs. So it is not following the MVC pattern per se, but a "traditional"
piece of code. From within that code I want to join back to the main MVC pattern. Response.Redirect works, but it is not neat, and is there a better way?
hc1
Member
199 Points
231 Posts
What MVC features available to normal class files?
Apr 05, 2012 11:07 AM|LINK
I am new to MVC.
In a standard MVC 3 project in VS 2010, what can normal class files, eg Class1.cs1, use to make use of the MVC pattern? The files I am talking about are not the controller or view or model. They are doing some common tasks, but they still need to interact with the rest of the application. In these files, how do I use routes or call the controller actions? Do I use httpContext only and code in the old web form way?
Thanks.
ignatandrei
All-Star
134960 Points
21632 Posts
Moderator
MVP
Re: What MVC features available to normal class files?
Apr 05, 2012 11:08 AM|LINK
Please elaborate your needs.
jsiahaan
Contributor
2316 Points
589 Posts
Re: What MVC features available to normal class files?
Apr 05, 2012 11:46 AM|LINK
Hi,
Welcome to the world of MVC,
You better start from the following sample: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc
Have fun
Indonesian Humanitarian Foundation
hc1
Member
199 Points
231 Posts
Re: What MVC features available to normal class files?
Apr 05, 2012 12:30 PM|LINK
For example, in one of my methods in Class1.cs I need to do a HttpContext.Current.Response.Redirect ("...."). Should I use the Redirect method? Or is there some way I can call a controller action from this piece of code?
Thanks.
francesco ab...
All-Star
20912 Points
3279 Posts
Re: What MVC features available to normal class files?
Apr 05, 2012 12:59 PM|LINK
HttpContext.Current.Response.Redirect ("....")......need just to agg the namespace the HttpContext is contained in...and calling HttpContext.Current.Response.Redirect ("...."),,,,,,you can do it.
However it is not good practice to call Web Environment function in Business classes
Mvc Controls Toolkit | Data Moving Plug-in Videos
ignatandrei
All-Star
134960 Points
21632 Posts
Moderator
MVP
Re: What MVC features available to normal class files?
Apr 05, 2012 01:41 PM|LINK
the class should anonunce( event?!) that the web should perform a redirect, not to perform itself...
hc1
Member
199 Points
231 Posts
Re: What MVC features available to normal class files?
Apr 05, 2012 03:06 PM|LINK
Thanks, I think that is what I need. How to do it? What is announce?
francesco ab...
All-Star
20912 Points
3279 Posts
Re: What MVC features available to normal class files?
Apr 05, 2012 03:12 PM|LINK
Also something written into a property that says to the controller to perform a redirect
Mvc Controls Toolkit | Data Moving Plug-in Videos
ryanw51
Contributor
2363 Points
511 Posts
Re: What MVC features available to normal class files?
Apr 05, 2012 03:50 PM|LINK
return some sort of result (string, enum, bool, int, class, etc) that tells the controller where to go next. The flow looks like this:
request -> controller -> business layer -> controller -> response
[HttpPost] public ActionResult Example(Employee employee) { bool redirect = _service.DoBusinessStuff(employee); if(redirect) { return RedirectToAction("Index"); } return View(); }hc1
Member
199 Points
231 Posts
Re: What MVC features available to normal class files?
Apr 06, 2012 11:16 AM|LINK
Thanks for all the responses.
I forgot to mention that the execution of my code does not originate from a controller action. It is from an overriden method (FormsAuthentication_OnAuthenticate) located in Global.asax.cs. So it is not following the MVC pattern per se, but a "traditional" piece of code. From within that code I want to join back to the main MVC pattern. Response.Redirect works, but it is not neat, and is there a better way?