How to handle callbacks in asp.net? - asp.net-mvc

Are there different callbacks available in asp.net like rails provide before_validation, before_update, before_save and before_destroy?
How to handle this scenarios in asp.net?

Take a look at the concept of Filters in ASP.NET MVC and also to the following 4 methods in the controller base class: OnActionExecuting, OnResultExecuting, OnAuthorization, and OnException.
http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onactionexecuting.aspx-
Update
On a second though I just realize that you might be looking for methods at the Model level and I answered from the Controller level. Unfortunately from what I understand that ASP.NET MVC does not provide as much at the Model level as Ruby On Rails does.

It looks like the link is broken so i'll pitch in.
In MVC you would use Action Filters
http://www.asp.net/mvc/tutorials/understanding-action-filters-cs
edit (I see you tagged as mvc but anyhow)
In Web Forms the onLoad method is run before each page action
Using a base class can span it over multiple pages

Related

Base Controller method or Utility helper method

On my MVC app, I am planning to create a generic method for Web Api's. This can be achieved either creating methods on Base Controller or methods in Utility helper class. Which is preferred way Base controller (or) Utility Class?
And also want to know on what scenario's Base Controller is useful.
ASP.NET MVC provides several alternatives to common problems (e.g. logging, exception handling, authorization) using filters or allowing your own implementation of a certain class. Usually a base controller is not the best approach since it is very likely the framework already handles the issue by other means.
Take a look at this answer for how to deal with common problems in ASP.NET MVC: https://stackoverflow.com/a/6119341/1942895

classic asp page posting to mvc3 controller action

I am currently working with integrating a classic asp site with MVC3. I have some questions on some areas of the integration that I would like some feedback on.
Firstly, I have a asp page posting to an MVC controller action. I have very little scope to modify the asp page. I want to take the form fields posted from the asp page and map them in to a model object. The posted values have obscure names such as "my_name" which I want to map to Name property on the model object. Is the best way of doing this via a Model Binder or is there an alternative?
Next question I have is a follow on from the previous, I am concerned with any cross site scripting so want to check the values of the posted variables to be valid and contain no strange characters etc. Is there something built in to MVC3 that does this out of the box?
When the asp page posts to the controller action, I would like to show a waiting icon while the controller action is processing as the controller action could take 10 seconds plus as it must call external systems etc. Therefore I don't want the post to seem as its hanging. Is it possible to wire up the controller action to return a view with a waiting icon, while the main body of the action is processing in the background and once complete redirects to another page?
Is the best way of doing this via a Model Binder or is there an
alternative?
The best way of doing is a model binder. You can have a custom model binder to take care of the ASP scenario that maps the my_name to Name. Mostly you should have a separate action to handle the requests coming from classic asp and you can link the custom model binder to only this action.
Is there something built in to MVC3 that does this out of the box?
The request validation is enabled as default in MVC. So if an user tries to post a script block to the action MVC will throw exception. Of course you can switch off request validation by decorating the action with ValidateInput(false) if you need.
For long running actions you have to use asynccontrollers.

Specifying that authorization is required

I'm somewhat new to MVC (but not ASP.NET). Is there a similar concept in MVC as there is in ASP.NET to specify URL authorization?
The question is really related to Facebook C# SDK - they have introduced the [CanvasAuthorize] attribute, which applies to a controller in the MVC app. How can I apply [CanvasAuthorize] to a set of controllers without attaching this attribute to each one?
Thanks,
Dan
You could have all those controllers derive from a base controller and then decorate this base controller with the attribute which will make it apply to all controllers and actions. In ASP.NET MVC 3 you also have the possibility to use global action filters and custom filter providers.

.net mvc design ajax calls (Where to put ajax methods)

Hi
When migrating from ASP.NET to MVC ASP.NET it looks like the MVC is more AJAX friendly.
but still I run into design issue,
Does someone knows Microsoft intention about the design when calling AJAX methods?
Where do I need to put this methods by default, in a separate controller, the same controller?
Is there any kind of official info about it?
Thanks
I don't think that there is any official best practices. Personally I like to follow RESTful conventions when organizing cotrollers and actions no matter how those actions are consumed (AJAX or not).
You might wanna try on having a review on some asp.net samples here. This will give you some ideas. :)
I would suggest taking SessionState into consideration when making the choice of method placement.
For instance, I would move ajax actions into a separate controller if I am using session state in regular actions, but not in ajax actions (which makes sense for me) and I would like the ajax methods to execute asynchronously. Then I put those ajax methods in a separate controller and mark the controller as [SessionState(SessionStateBehavior.Disabled)] (or ReadOnly). I have found this to be a great improvement in terms of performance.
Note, that you use Session when you use TempData, ViewData or ViewBag variables.
SessionState is explained here:
ASP.NET MVC and Ajax, concurrent requests?

Confused on MVC in rails

I am a bit confused on what to write where in Rails.
Ideally, I will be having a view, a controller, a model. Model should be having all the business logic. But in most of the Rails applications I've seen, I've seen most of the business logic written in the Controller files.
Should we call them as controller? And what about View-models. I am talking about the datamodels that are associated for a view. I am having JSF and Swing in my mind when I say this. There every view has a datamodel associated with it, usually a bean. But here, we don't have anything like that or I am ignorant? And what about service layers, How do I implement them in my Rails code.
To summarize my questions
Why are business logic being
written in Controllers in most of
Rails code? Is this a good practice?
How to incorporate the view-model in
rails, ie, data-models for view?
Where to put service layers in a Rails app?
Thanks
To summarize my answers:
The new standard for Ruby on Rails is to place business logic in the model. This is also known as fat model skinny controller.
Rails doesn't enforce this approach of one data model but you can implement it if you choose. The basic scaffolding does something similar.
The service layers will most likely be stored within rack middleware. This allows for general filtering of requests and responses.

Resources