Someone ask following question to me in interview, to which I cannot able to answer. Somebody please help me.
Can we create Static ActionResult method in controller?
If yes. What is the benifits? and if no. What is the reason?
If "ActionResult method" is a method that returns ActionResult, you can declare it static, of course, but it will not be called when you initiate that action. When calling some action, ASP.NET MVC creates an instance of the controller (using ControllerFactory), and since your method is static, it is not related to any instance, and thus, it will not be called by MVC
Don't think it is a good idea to create a static action method, as you will no longer have access to all the controller properties, such as controllercontext, etc.
Related
I've been working with quite some Web API projects now and find myself bumping into the same problem every time and that's when I do a POST or GET the value / model etc is null or I get a 404.
There is a checklist like:
- did I use the correct content-type?
- has routing been set up correctly
- is the signature of the model that I'm posting really the same as the model that the endpoint accepts?
It would be nice if there is a trace one could follow where it fails. Now it just looks like a black box, you put something in and it works or not, if it doesn't: see checklists or SO.
Is there something that you can setup in Web API so you can debug the model binding process?
I would implement action filter.
One of the methods that can be overridden there is :
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
In this action you can check the response status and if it is an error to perform all your checks
This article could be a good starting point
I want to add some code to log the time spent in each action call in a controller. I saw a suggestion of creating a Stopwatch inside the OnActionExecuting method of the controller and stoping it OnActionExecuted, which seems fine to me.
What I want to know is where do I have to add the started Stopwatch object so it can be read back once OnActionExecuted is called.
I was thinking on adding it to the Session, but I'm guessing this might have issues if there are simultaneous requests from the same session.
What is the best place to store this data?
Thanks
You can use HttpContext.Current.Items for objects that are related to a single request.
https://web.archive.org/web/20201202215202/https://www.4guysfromrolla.com/articles/060904-1.aspx
I am trying to access a text file in asp.net mvc3 test project, the common way I know to access it is through HttpContext class like this
HttpContext.Current.Server.MapPath("Temp.txt");
But it returns "Current" as null!
After a little search I found few reasons of Current being null, in my case it is I guess using a router handler to route requests.
But not getting the way to do it for my case.
Can anyone help me on that?
HttpContext is not available on test context, only on web enviroment. You need an HttpContext stub. Avoid this direct dependence.
http://vkreynin.wordpress.com/2009/03/23/stub-htttpcontext/
I am new to MVC and TDD, so if it is a stupid question please spare me :)
What I am trying to do is , I have created a SignOn controller and I just want to write a Test for that controller. The thing is SignOn controller does Response.Redirect internally, that works fine if it is an proper request, but when I run my test it fails as there is no response object.
so how can I test my SignOn controller, which redirects internally ??
When you want to do a redirect you should use the actionresult: Redirect("url"). In your test you can check if the actionresult is of type RedirectResult and/or if it has the right property values.
Your action method should return a RedirectResult. If you know exactly what the URL should be, you can test the Url property.
I am not a TDD guy but something tells me that HttpContextBase is your friend : http://msdn.microsoft.com/en-us/library/system.web.httpcontextbase.aspx
Here is a good resource on that.
http://weblogs.asp.net/gunnarpeipman/archive/2011/07/16/using-moq-to-mock-asp-net-mvc-httpcontextbase.aspx
Hope this helps.
One of the patterns you can use for testing navigation code inside a controller/presenter is the ApplicationController.
Have a look at the following descriptionPEAA: Application Controller
We have implemented the Aplication Controller in our ASP.NET app.
ApplicationController.Navigate(view: "LoginScreen", argument: userId);
The ApplicationController checks which NavigationWorkflows are registered and delagetes the navigation to the correct class.
In your unit tests you can mock the NavigationWorkflow with something like rhino mocks and pass this to your ApplicationController. Then you can check if the correct navigation methods are called on your workflow.
So instead of calling Response.Redirect, you delegate the navigation responsibility to a separate class that can be replaced with unit testing.
I've read David Hayden's great post on MVC 3 Remote validation.
However there is presented what you should do to enable remote (javascript) validation. If the user has javascript disabled the post would still be made even if data is not valid. Therefore a server-side validation should occur.
How could we make this check as DRY (Don't Repeat Yourself) as possible? Of course, including the same check code in the post action as in the remote validation action (or just the same call) can work but I am wondering if a one-liner or something more elegant is available.
Perfectly acceptable answers include "no, it can't be done". :)
See my MSDN article How to: Implement Remote Validation in ASP.NET MVC
I use the remote client validation code in the HttpPost Create method to test server side when JavaScript is disabled.
[HttpPost]
public ActionResult Create(CreateUserModel model) {
// Verify user name for clients who have JavaScript disabled
if (_repository.UserExists(model.UserName)) {
ModelState.AddModelError("UserName", ValidationController.GetAltName(model.UserName, _repository));
return View("Create", model);
}
It 'can' be done.. but you would need to write your own custom attribute that basically emits for client side and is validated server side. For me I just extract the validation code into a method and check on the server.
Something similar came up recently as well:
Prevent form from submitting when using unobtrusive validation in ASP.NET MVC 3
I wonder if one couldnt inherit from the remote attribute and add their own server side code them. hmm.. maybe I'll have to try this.
I would be happy though if someone here said they already did this : )
I have done this, it's a bit of a long solution, so it's all available on my blog here:
http://www.metaltheater.com/tech/technical/fixing-the-remote-validation-attribute/
I had to create a new subclass of the RemoteAttribute class, create my own custom model binder by inheriting from DefaultModelBinder, and then use reflection to call the validator on the controller.