Asp.net MVC 2: Is it possible to call a specific action of a controller for a User Control - asp.net-mvc-2

I'm just begin upgrade my asp.net site from Webform to MVC 2. So far it's more clear and lighter than Webform. I'm stucking on the part of rending an User Control in different actions.
My User Control named Banner.ascx is placed in Site.Master. This Banner.ascx get url from DataView["BannerUrl"] which is set in HomeController > Index action. This will run OK when I point URL to /Home/Index. Now I want this Banner.ascx control get DataView only from HomeController > Index whatever action I'm pointing to, for example when point URL to /Article/Detail/1 I want the Banner.ascx run action Index of HomeConttroller to get DataView["BannerUrl"]
Any response is appreciated. Thanks in advance.

You can call the MVC Controller the same way you would call any other server-side code. You can access the registered IControllerFactory instance via ControllerBuilder.GetControllerFactory(). Then you can use the controller factory to get an initialized instance of the IController instance by passing it the route values in the RouteData of the RequestContext parameter (these would be controller = "Home" and action = "Index". IController.Execute() will then execute the Action the same way that the MVC life cycle would if it received a request.

Related

Zend AjaxContext, _redirect and hash navigation

first post in SO, even though I've been browsing it for years now to solve those mind-blowing and not so much coding problems.
What I want to do is:
* Use hash navigation (#!/).
* Use Zend controller actions, not php files.
* Load these actions through javascript/jQuery.
So far, I've got this working:
indexController, several Actions, each attached to AjaxContext via addActionContext(), I can call them though my javascript/jQuery file via "hashchange" plugin jQuery(window).hashchange(function(){ bla bla }). I can cycle through actions just fine.
But I want to redirect the user to a login page if he/she is not logged in, which brings me to my issue: How can I achieve that? The redirection is made to another controller (login controller, login action). I was trying something like $this->_redirect('/#!/login/login'); w/o any luck (yes, I've set up an AjaxContext in that controller's init). I keep getting a redirection error ("The page isn't redirecting properly"). If I just type in the address bar "/#!/login/login" I get everything display properly.
Anyway, thanks in advance!
Cheers
Now this starts to get complicated if you ever introduce other non-ajax contexts, but you could add the Ajax context to the Error Controller. Then have the error controller return JSON for the unauthenticated exception if the active context was AJAX (and keep the redirect if the default context was active). Your JS would then listen for that specific error provided by the JSON and manually bounce the user to the appropriate login URL.

Replacing MVC's built in User objects with Facebook Connect

I have an ASP.NET MVC 3 project where I have NO local user management. I have intergratred Facebook Connect successfully. While this works, it makes my Controllers and Views messy and verbose.
I'd like to replace the default objects such as the User object exposed by Controllers and Views to return my FacebookUser object instead.
Anyone have a better solution than having my Controllers digging around in FacebookWebContex.? It just feels dirty.
Not quite sure which facebook library you are using. But if you impliment IPrincipal and IIdentity in your FacebookUser object, you will be able to set HttpContext.Current.User to that FacebookUser which will allow you to pull that FacebookUser instance from the User property in the controller.

Zend Controller Plugins vs Subclassing Action Controller

I've got a pretty standard ACL system in my application. There's a Login controller and a bunch of other controllers redirecting back to Login if user is not authorized. I use a Controller Plugin for checking the ID and redirecting and I obviously don't want Login controller and Error controller to perform such a redirect.
Now I've read several times that using Controller Plugins is a better practice than subclassing the Action Controller. Yet what I see is it's much easier to extend all my controllers from this abstract base controller class which performs the necessary checking in its init method, except for the Login controller which extends Zend_Controller_Action directly.
So the question is, is there a way to attach the plugin to the controllers selectively? Of course I can always make an array out of certain controllers, send it to a plugin through a setter method and do something like:
$controller = $request->getParam('controller');
if (count($this->exceptions))
if (in_array($controller, $this->exceptions)) return;
//...check ID, perform redirect, etc...
Yet something tells me it's not the best way doing it.
And advices?
EDIT 1: #Billy ONeal
Thank you for your reply, but I don't quite catch. I can do
public function init()
{
$this->getRequest()->setParam('dropProtection', true);
}
(or run some method that sets some private variable of the plugin) in my login controller, and then say if 'dropProtection' is not true then check the user ID. But the actual dispatch process looks like this:
Plugin::dispatchLoopStartup
Plugin::preDispatch
Controller::init
Plugin::postDispatch
Plugin::preDispatch
Plugin::postDispatch
Plugin::dispatchLoopShutdown
So I cannot check this 'dropProtection' param earlier than in Plugin::postDispatch and that's a bit late. (by the way, why the preDispatch and postDispatch are being called twice?)
If you want to do it earlier, I think you can use the first method (passing an array of exceptions to the plugin) and test the module name or the controller name in routeShutdown.
Personnaly I use an action helper to check the auth in all my actions. It's more flexible and give me more control. It's only one line for each private action.
And DON'T SUBCLASS your action controller. I did it on one of my project and now my base class is a piece of shit. Use action helper instead.
is there a way to attach the plugin to the controllers selectively?
Of course. Just don't register the plugin if the request doesn't contain the parameters you're looking for. Alternately, assume all pages are protected, and have those pages which should not be protected call some method on your plugin during the init stage.
If you want to protect just a single controller, you could reverse that -- have the plugin only take action if there's some method called during the init stage.
Finally, you could make the entire logged-in section of the page it's own module, which would allow you to have the plugin check for that module before checking credentials and redirecting.

ASP.NET MVC passing data between forms

Im pretty new to ASP.NET MVC, trying to figure out my way around.
Currently i return a ViewModel which has a IEnumeable Events as its only property. This ViewModel is StronglyTyped to a UserControl which dislays the IEnumable Events in tabular form.
One of the properties of the Event Model is an XElement, which contains some XML loaded from the DB.
Now i've added a link to the end of the tablular data to be able to view the XML in a separate page. How do i pass this data to another page for viewing?
I would post a request back to the server with some sort of Id for the Event-object and have the receiving end send back the XML related to that Id.
if you're looping through the Event objects in your IEnumerable, you can do something like:
<%= Html.ActionLink("GetXml", "Events", new { id = currentEvent.Id }) %>;
Now create an Action on your EventsController (given that you have one) like so:
public ActionResult GetXml(int id)
and retrieve the XML to pass back to the View
There are basically two ways of bringing data from one page to another using ASP.NET MVC (or any other language/framework which follows the HTTP protocol):
Sessions: Use a session to store the data you need, and load it back up at the next page.
Post the needed data back to the server. This way, the server can hold it and display it on the next page. Posted data usually comes from input or textarea elements. If you use input type="hidden" you can give it a value which represents your data. This way, you can post it back and forth till you arrive where you want.
Besides what Arve is advising, you could also consider TempData.
If you use the Get-Post-Redirect/Forward concept for you app, you could do something like:
GET - Initial Request comes in, Server responds with View and model data. User selects an item which leads to ...
POST - User selects one of the items from #1, triggering a post. That particular item can be fetched from repository, placed in TempData and then...
REDIRECT/FORWARD - The redirect collects the information out of TenpData and uses it as the model for the new View.
here is an example http://www.eworldui.net/blog/post/2008/05/08/ASPNET-MVC-Using-Post2c-Redirect2c-Get-Pattern.aspx

ASP.Net MVC 2 Forms Authentication cookieless = "UseUri" while submit authorization fails

I just started working with ASP.Net MVC 2.
I created a new ASP.Net MVC application and created one vehicle controler with a database table connected with LINQ. Then created forms authentication mechanism for the application and tried to use the uri instead of cookies it was working smoothly but when i submit the form by creating a "Create" view from the controler using the utility it just dont work. The autherization got failed and asking to enter the user name and password again.I had created the authorization mechanism by adding Authorise attribute to the Controller so as to get authorized for all the actions.
namespace MVCNEW.Controllers
{
[Authorize]
public class VehicleController : Controller
{
But if i use the cookies instead of uri it works fine.
Thanks in advance...
Please see http://forums.asp.net/p/1517391/3634908.aspx for an official response.
Summary: Cookieless Session support is essentially obsolete, and the MVC framework isn't likely to include additional support for it.
I found the problem and a solution.
This was due to some error in the framework. They are not creating the Uri string for the Form action while calling
Html.BeginForm()
But if we make it call overloading of this method like the providing the Controller name and Action name it is working fine.
view plaincopy to clipboardprint?
Html.BeginForm("Create","Vehicle")