Note: All this data has been copied from various sources for the purpose of knowledge sharing. Data content can be either as it is or with little modification.
---------------------------------------------------------------------------------------------------------------------
Where are the routing rules defined in an asp.net MVC application?
In Application_Start event in Global.asax
---------------------------------------------------------------------------------------------------------------------
Where are the routing rules defined in an asp.net MVC application?
In Application_Start event in Global.asax
protected void Application_Start() { .....
RegisterRoutes(RouteTable.Routes); }
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); routes.MapRoute( "TwoParameters", // Route name "{controller}/{action}/{parentId}/{id}", // URL with parameters new { controller = "Home", action = "Index", parentId = "", id = UrlParameter.Optional } // Parameter defaults ); }What are the different return type controllers action method supports in ASP.NET MVC?
There are total nine return types we can use to return results from controller to view. The base type of all these result types is ActionResult. 1. ViewResult (View) : This return type is used to return a webpage from an action method. 2. PartialviewResult (Partialview) : This return type is used to send a part of a view which will be rendered in another view. 3. RedirectResult (Redirect) : This return type is used to redirect to any other controller and action method depending on the URL. 4. RedirectToRouteResult (RedirectToAction, RedirectToRoute) : This return type is used when we want to redirect to any other action method. 5. ContentResult (Content) : This return type is used to return HTTP content type like text/plain as the result of the action 6. jsonResult (json) : This return type is used when we want to return a JSON message. 7. javascriptResult (javascript) : This return type is used to return JavaScript code that will run in browser. 8. FileResult (File) : This return type is used to send binary output in response. 9. EmptyResult : This return type is used to return nothing (void) in the result.Action Filtering in ASP.NET MVC ApplicationsAction Filters allow us to add pre-action and post-action behavior to controller action methods.n action filter is a .NET class that inherits from FilterAttribute or one of its subclasses, usually ActionFilterAttribute, which adds the OnActionExecuting, OnActionExecuted, OnResultExecuting, and OnResultExecuted methods, providing hooks for code to be executed both before and after the action and result are processed.ASP.NET MVC provides several action filters out of the box:
- Authorize – checks to see whether the current user is logged in, and matches a provided username or role name (or names), and if not it returns a 401 status, which in turn invokes the configured authentication provider.
- ChildActionOnly – used to indicate that the action method may only be called as part of a parent request, to render inline markup, rather then returning a full view template.
- OutputCache – tells ASP.NET to cache the output of the requested action, and to serve the cached output based on the parameters provided.
- HandleError – provides a mechanism for mapping exceptions to specific View templates, so that you can easily provide custom error pages to your users for specific exceptions (or simply have a generic error view template that handles all exceptions).
- RequireHttps – forces a switch from http to https by redirecting GET requests to the https version of the requested URL, and rejects non-https POST requests.
- ValidateAntiForgeryToken – checks to see whether the server request has been tampered with. Used in conjunction with the AntiForgeryToken HTML Helper, which injects a hidden input field and cookie for later verification (Here’s a post showing one way you can enable this across the board).
- ValidateInput – when set to false, tells ASP.NET MVC to set ValidateRequest to false, allowing input with potentially dangerous values (i.e. markup and script). You should properly encode any values received before storing or displaying them, when request validation is disabled. Note that in ASP.NET 4.0, request validation occurs earlier in the processing pipeline, so in order to use this attribute, you must set the following value in your web.config file:
<httpRuntime requestValidationMode="2.0"/>







