https://www.c-sharpcorner.com/UploadFile/cda5ba/security-feature-in-mvc/
Monday, October 10, 2022
How to Handle Exception in Asp.NET MVC
5 Ways to do MVC Error Handling
- Web.Config customErrors
- MVC HandleErrorAttribute
- Controller.OnException method
- HttpApplication Application_Error event
- Collect exceptions via .NET profiling with Retrace.
- Gracefully handling errors and show your users a friendly error page
- Logging errors so that you are aware of them and can monitor them
- Web.Config customErrors:
<system.web> <customErrors mode="On" defaultRedirect="~/ErrorHandler/Index"> <error statusCode="404" redirect="~/ErrorHandler/NotFound"/> </customErrors> <system.web/>
Saturday, October 8, 2022
When to Use filter with real time example and Types.
The ASP.NET MVC framework supports four different types of filters:
- Authorization filters – Implements the IAuthorizationFilter attribute.
- Action filters – Implements the IActionFilter attribute.
- Result filters – Implements the IResultFilter attribute.
- Exception filters – Implements the IExceptionFilter attribute.
Filters are executed in the order listed above. For example, authorization filters are always executed before action filters and exception filters are always executed after every other type of filter.
Authorization filters are used to implement authentication and authorization for controller actions. For example, the Authorize filter is an example of an Authorization filter.
Action filters contain logic that is executed before and after a controller action executes. You can use an action filter, for instance, to modify the view data that a controller action returns.
Result filters contain logic that is executed before and after a view result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.
Exception filters are the last type of filter to run. You can use an exception filter to handle errors raised by either your controller actions or controller action results. You also can use exception filters to log errors.
What is HTTP Handler and HTTP Module.
HTTP module and HTTP handler are used by MVC to inject pre-processing logic in the request chain.
- HTTP Handlers are extension based pre-processor whereas HTTP Module are event based preprocessor.
- For example: if you want to change how
jpgfiles are processed, you will implement custom HTTP handler versus if you want to execute additional logic during processing of the request, you will implement a custom HTTP module. There is always only one HTTP handler for a specific request but there can be multiple HTTP modules.
- If you want your functionality to only be executed once per Http Request, you should use an HttpModule.
Saturday, August 6, 2022
Using TempData, Peek And Keep In ASP.NET MVC
- If you set value for TempData and do not read the value then the data will be available for next request.
- If you set value for TempData and read in View then the data will be deleted or will be null.
- If you read TempData in the first request and want to keep the value for the next request then use 'Keep' Method.
- If you read the TempData using 'Peek' then value persists for the next request also.
Explain Dependency Resolution?
Dependency Resolver again has been introduced
in MVC3 and it is greatly simplified the use of dependency injection in your
applications. This turn to be easier and useful for decoupling the application
components and making them easier to test and more configurable.
What “beforeFilter()”,”beforeRender” and “afterFilter” functions do in Controller?
beforeFilter(): This function runs before every action in the controller. It’s the right place to check for an active session or inspect user permissions.
beforeRender(): This function is called after controller action logic, but before the view is rendered. This function is not often used but may be required if you are calling render() manually before the end of a given action.
afterFilter(): This function is called after every controller action and after rendering is done. It is the last controller method to run.
Sunday, July 17, 2022
What is ViewModel with Example and Uses and benifits.
What is ViewModel?
In ASP.NET MVC, ViewModels are used to shape multiple entities from one or more models into a single object. This conversion into single object provides us better optimization. You can see the concept of ViewModel in the image below.
Where we should use ViewModel?
We can use ViewModel in the following scenarios,
- Managing or creating dropdown lists for a specific entity
- Creating Master-Details View in data-driven websites
- Used in showing shopping carts on the website
- Used in Pagination
- Used in a website to show User profile widget
- Used to make Dashboards for integrating multiple data into one place
- Used as an Aggregate Root to make reports in Domain Driven Design (DDD)
- These are the complex scenarios in which ViewModel can be used for better maintainability, reliability, and testability of code.
Sunday, May 22, 2022
What is Bundling and Minification in MVC?
Bundling and minification are two new techniques introduced to improve request load time. It improves load time by reducing the number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript).
Bundling
It lets us combine multiple JavaScript (.js) files or multiple cascading style sheet (.css) files so that they can be downloaded as a unit, rather than making individual HTTP requests.
Minification
It squeezes out whitespace and performs other types of compression to make the downloaded files as small as possible. At runtime, the process identifies the user agent, for example IE, Mozilla, etc. and then removes whatever is specific to Mozilla when the request comes from IE.
What is ViewStart?
Razor View Engine introduced a new layout named _ViewStart which is applied on all view automatically. Razor View Engine firstly executes the _ViewStart and then start rendering the other view and merges them.
@ {
Layout = "~/Views/Shared/_v1.cshtml";
}
< !DOCTYPE html >
< html >
< head >
< meta name = "viewport"
content = "width=device-width" / >
< title > ViewStart < /title> < /head> < body >
< /body>
< /html>
Tuesday, May 3, 2022
WHAT ARE PARTIAL VIEWS IN ASP.NET MVC
Partial View is a subpage of Main View page that keeps reusable parts of web pages. If your Main Page is too large then you can divide it into several pieces and store content into a partial page with a logical name. After that, you can call this partial page inside your main view page.
More Facts about Partial Page:
1. It is an effective way to breaking up large view pages into smaller components.
2. A partial page is reusable so keep common content in the partial page and used wherever you want.
3. You can recall partial page inside a view page using following ways:
@Html.Partial("ViewName") // A view with this name must be in the same folder @Html.Partial("ViewName.cshtml") // Locate the view based on the application root // Paths that start with ".php" or "~.php" refer to the application root @Html.Partial("~/Views/Folder/ViewName.cshtml") @Html.Partial("/Views/Folder/ViewName.cshtml") // Locate the view using relative paths @Html.Partial("../Account/LoginPartial.cshtml")
Use below link for more infohttps://www.completecsharptutorial.com/asp-net-mvc5/adding-partial-views-pages-in-mvc-5-with-example.php
Saturday, April 30, 2022
Various Method in TempData
Various Method in TempData
- Keep : – Marks all keys in the dictionary for retention.
- Keep (with Key): – Marks the specified key in the dictionary for retention.
- Peek (with Key): – Read data from key without marking the key for deletion.