MVC :: Action Filter Attribute Breaking Change?
Jan 20, 2011
I have an app I am transitioning to MVC3 to see what the upgrade path is essentially. I've hit a snag. I believe I've run into a change that may be due to the changes for Dependency Injection in action filter attributes, but I have been unable to track down the cause, and the "correct" solution.This project uses StructureMap and NHibernate. The NHibernate session is started at the beginning of each request, and ends at the end of each request, thanks to a small module. This is just to give an idea of the stack involved...o I have a few custom Authorize attributes (Inheriting from AuthorizeAttribute) that perform some more complicated authorization logic for specific actions that they decorate. Part of their functionality involves using two repositories which they must either (a) have injected, or (b) get from the StructureMap ObjectFactory themselves. In MVC2 I was using option b, so that the CONSTRUCTOR of the attribute looked like this:
[Code]....
This worked fine, as in MVC2 the constructor was called each time the attribute was evaluated. This BREAKS in MVC3, where it would appear that this is no longer the case. Due to this apparent change, the FIRST time this attribute is evaluated, it runs fine, but each subsequent time, any calls in the repositories fails because it claims the NHibernate Session object hasn't been started! The reason being, as far as I can tell, that the CustomAuthorizeAttribute is created once, and then held in whatever state it exists in at that point for the lifetime of the application... Which means that the dependencies inside of it (Which SHOULD go out of scope at the end of each request, and be reinstantiated / fetched at the beginning of subsequent requests), are instead persisting with it becoming stale after the first evaluation.After banging my head against the desk for a few hours, I realized that simply moving these calls to the ObjectFactory into the AutorizeCore method made everything work again.Now, all of this was of course, a hack, because there wasn't a good easy to do real dependency injection into attributes in MVC2. I hear that is changed in MVC3, and I am wondering if this behavior is in response this change, and if so, what do I need to do to make this work?
View 3 Replies
Similar Messages:
May 24, 2010
I am facing challenge of changing action attribute of html form tag. As my application is on shared hosting (medium trust) environment of godaddy server, I cannot use reflection to get control adapter of html tag page. So, I cannot use the following code in my Url Rewriting module.
[Code]....
Is there any otherway to implement the same functionality without reflection?
View 4 Replies
Mar 17, 2010
I have decorated my base controller with a couple of action filters. They work fine.
One of those filters sets up the request - does things like set the culture based on the domain, etc.
I also have a handful of actions that require authorization using the Authorize attribute.
My problem is that when an user attempts to request a page they are not authorized to access, the authorization filter kicks in and redirects them to a page telling them that they cannot vie the page.
The issue is that the action filters never run so the culture and other request data is never set. This effectively causes language to be wrong in the view and other data to be missing.
I know that authorization filters run first but my question is this: How can I design this such that I can ensure that certain methods are always run before the view is returned, regardless of the authorization.
View 1 Replies
Aug 25, 2010
does a action filter override a controller filter?
View 2 Replies
Jun 21, 2010
Does any one know how to dynamically add ActionFilter to an Action?
View 3 Replies
Apr 1, 2010
I had the same problem and used this posted to fix some of it.Now I had it on
SqlDataSource1.SelectParameters.Add("dateListedSince", TypeCode.DateTime, Date.Now.AddDays(-30))
any ideas how to fix that?
View 2 Replies
Apr 22, 2010
I have just upgraded a large project to MVC 2 (50+ controllers) and whilst the project builds fine I am having issues in several places because it looks like the way the Controller.UpdateModel method handles emtpy strings has changed.
In MVC 1 it set the associated property to an empty string but in MVC2 it's setting it to null.
View 2 Replies
Apr 22, 2010
I have just upgraded a project from MVC 1 to 2 and I have come across an issue with fields updated via UpdateModel which contain an emtpy string.
In MVC 1 the associated property would be set to String.Emtpy or "", but in MVC 2 it's set to null. This is causing me all kinds of problems
View 1 Replies
Feb 9, 2011
I am trying to return different views with xml and json result when say -/API/Security/Authentication/login.xml is passed or
API/Security/Authentication/login.json is passed . I have adde mapped routes for this path.
I have used the ActionFilter attribute for the Login Action Result to automatically check for xml or json and then return the appropriate view.
what to put in the Login.aspx file to render appropriate xml or json based on the above url passed ?
The code is below:
[Code]....
View 2 Replies
Dec 10, 2010
I just learned about actionfilters yesterday and thought it would be great if i could put my meta data into a action filter and change the page title, page description etc from 1 location based on a value from the controller.
I've getting so far, but cannot seem to work out how to add the default value to the controller and pass that value to my actionfilter.
[Code]....
View 10 Replies
Feb 17, 2010
here's my situation - I've created some ActionFilterAttributes that I want to apply to a custom Controller class that my other controllers can inherit from. The problem is, I need to set a property on the attribute from the final inheriting controller. So I have something like this :
[Code]....
[Code]....
View 2 Replies
Jul 20, 2012
can we create form action in asp.net or not ?
Like here i use action="insert_cont.php"
<form id="form1" name="form1" method="post" action="insert_cont.php">
</form>can i create action in asp.net for it ??
View 1 Replies
Jul 14, 2010
I need to create application wide error handling mechanism. I was evaluating Action Filter for that matter. But- Action filter will just exectute against actions/controller- what about error at custom view engine or Extension method for HTML helper.: to handle them i need to implement traditional Asp.net mechanismCan anyone suggest best common approach which can handle error for all Controller, View, Model or custom helpers if any.
View 3 Replies
Mar 4, 2010
If I have a Filter applied to a controller with a property defined as true.
Can I apply the same filter to one of its actions with value false to override the value for that specific action?
View 2 Replies
Jan 6, 2011
Is it possible to edit the content of a view from a action filter.
What I am working on is a Resourcemanager that I can use to manage
my style sheets, js and other resources. I want to be able to put
lines in the the view any where like Html.ResourceManager.AddScriptFile("MyScript")
or Html.ResourceManager.AddCSSFile("MyCSS")
and at the end of the view call Html.Render(); this would then place the content of my resources at this point. My problem is that I want to put the CSS stuff at the top of the view, but them problem is that it is renderend at the end of the view, so I tought that I could use a ActionFilter to move it to the top of the view.
1) What would be the best way to do this task ?
2) Is there any resources that describe the way that the MVC framework works in more detail ?
View 2 Replies
Feb 16, 2011
can anyone make it clear the main purpose of action attribute of form tag in html page?
View 2 Replies
Jan 27, 2011
I have a form action in an ASCX page set to an external URL
<form id="fLoginForm" runat="server" action="http://external.url" method="post" defaultbutton="bSignIn">
Inside there is a standard ASP linkbutton
<asp:LinkButton CssClass="btn" ID="bSignIn" runat="server" Text="Sign In" OnClick="bSignIn_Click" />
The event "bSignin_Click" never gets fired when I have ction="http://external.url" set on the form tag. However when I remove the action, it works as expected. I thought for runat='server' forms, the form would always post back? I need to read the URL from the action attrib and then redirect to it with some hidden input values also in the page.
View 4 Replies
Apr 12, 2010
After deploying a ASP.NET WebForms application to a production server some of the ajax calls has stopped working for me. After some debugging I found out that the following statement (in the internal client-method WebForm_DoCallback) was causing the problem:
xmlRequest.open("POST", action, true);
The problem seemed to be that the action-variable was empty so after checking the rendered html I found out that the form-tag rendered on the production server looks like:
<form method="post" action="" id="Form1">
However, on my developer machine I get the following:
<form method="post" action="default.aspx" id="Form1">
So, why would the action-attibute render on my dev.machine but not on the production server? This seems to be the case only for one specific web form, when I look on other pages the action-attribute renders correctly.
View 2 Replies
Mar 2, 2011
I need to get an image url using FileUpload Control. When i do it in the explorer it shows all files to select. But i need to show only ".jpg,.gif" files. How can i do it.
View 3 Replies
Jul 17, 2010
I have a custom actionfilter that strips out the whitespaces before the html is rendered in browser and it is working fine. However I have been unable to unit test the custom action filter. Ideally I want to do an assert on the sample html that all whitespaces have been removed. Code looks something like this.
[Code]....
View 1 Replies
Nov 17, 2010
I'm trying to get the current action method by this way.
ActionDescriptor actionDescriptor = filterContext.ActionDescriptor;
string actionName = actionDescriptor.ActionName;
var controllerType = filterContext.Controller.GetType();
var actionMethod = controllerType.GetMethod(actionName);
But if there're two action method with the same name, the AmbiguousMatchException will occur there. Without the ActionMethod property, how can I get the current action method now?
View 5 Replies
Feb 25, 2010
I thought this would be easy and maybe it is but it's eluding me.
In an action filter (MVC 2) I want to add/edit one of the parameters for the type I'm passing to my action method.
I have an action method:
[Code]....
Bar is a simple class for viewData:
[Code]....
In my BarFilter action filter OnActionExecuting method I want to add a value for paramA which is not set by the view so that Bar will be fully populated in my Foo action method. I've tried various methods of adding values to ViewData, ModelState, Model etc. but I always get a null returned in Foo.
View 3 Replies
Mar 7, 2010
I have a webpage with a form below -
<form action='https://www.sandbox.paypal.com/cgi-bin/webscr' method='post' name='frm'>
I have a button that does a postback to server then back to client and finally to the action attribute of the form element.When the page post back to the client the onload <script>document.frm.submit()</script> finally submits the form to the action attribute of the form.I would like to eliminate the unnessasay post back to the client.Below is the code the runs on the server.
If Page.IsPostBack =
True
Then [code]....
View 1 Replies
Aug 22, 2010
How can I define a Dynamic ViewModel property in MVC 3 inside an action filter?
I am looking to have something like but inside the ActionFilter and not in the controller:
[Code]....
View 2 Replies
Sep 27, 2010
i want to create filter in action method for filtering particular user from my database for the login program....
View 2 Replies