Posts

Showing posts with the label MVC

Setup Code first approach in .net core MVC application using entity framework

Image
In Previous article " .Net core MVC project structure explained step by step part 2 " we have discussed about .Net core MVC project structure. In this article we will be setting up the code first approach in .net core application. To control  and manage the entities we can opt for code first approach to design our database structure. To setup the entity framework in .net core app we need to add following nuget package to our projects. Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.Relational Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools Microsoft.EntityFrameworkCore.Design       I will show you how and where you need to use the above packages to setup the complete structure. I will use the same solution which i have used in previous article to setup the code first approach. Lets start the implementation. Go to solution explorer and right click on solution then click on new project then create the class library project and name it as

.Net core MVC project structure explained step by step part 2

Image
 In previous article " Net core MVC project structure explained step by step part 1 " I have discussed about creating MVC .net core project and its basic structure. In this article i will discuss MVC .net core project structure further.       Following is the project structure which i will be discussing in this article.     wwwroot: This is the directory where we can store projects static files for example css files, Javascript files, images, or any other binary files required for the project. Controllers: We can add the controllers of the project. Controllers are responsible for handling user request, do the processing and provide the appropriate response to user request.  Models: you can store your model files and dbcontext in this folder to add your business logic in application. Views: In this directory we can add views in  the form of chtml files(Razor engine)  appsetting.json: In this file we can configure the custom attributes required for the project for fine tuni

.Net core MVC project structure explained step by step part 1

Image
 To understand the .net core project structure we need to create the new MVC application. for that go to file menu and click on create a new project menu. select the Asp .net core web app template from the popup window as shown below.         Then click on next button and provide application name you want to create the project for and select the appropriate location where you want to create the project and click on next button as shown in below image. in next screen you can select the target framework and click on create button as shown below image.  Now right click on solution explorer and click on manage nuget package option and add any package you are going to use in the application. In my case i have installed the Nlog package in the sample project. below is the glimpse  of my solution in the image. so in solution explorer above you can see that there is dependencies section where following are the project dependency categories. Frameworks: The framework category contain the infor

Dependency injection in ASP.NET Core MVC controller explained step by step

ASP.NET Core MVC controllers should request their dependencies explicitly via their constructors. In some instances, individual controller actions may require a service, and it may not make sense to request at the controller level. In this case, you can also choose to inject a service as a parameter on the action method. With ASP.NET 5 MVC6, we get a feature called dependency injection baked right into the framework for us. Dependency injection is a technique that follows the Dependency Inversion Principle, allowing for applications to be composed of loosely coupled modules. ASP.NET Core has built-in support for dependency injection, which makes applications easier to test and maintain. ASP.NET Core's built-in support for constructor-based dependency injection extends to MVC controllers. By simply adding a service type to your controller as a constructor parameter, ASP.NET Core will attempt to resolve that type using its built in service container. Services are typically, b

Introduction of Asp.net MVC Basics for beginners

MVC is an architectural pattern which separates the representation and user interaction. It's divided into three broader sections, Model View Controller.  Below is how each one of them handles the task. View : The View is responsible for the look and feel. Model: Model represents the real world object and provides data to the View. Controller: The Controller is responsible for taking the end user request and loading the appropriate Model and View. Advantages of ASP.NET MVC Extensive support for TDD. With asp.net MVC, views can also be very easily unit tested. Complex applications can be easily managed Separation of concerns. Different aspects of the application can be divided into Model, View and Controller. ASP.NET MVC views are light weight, as they do not use view-state. It provides extensive support for URL Routing that helps to make friendly URLs (means friendly for human as well as Search Engines). Support for existing ASP.NET features like membership

Security Exception in ASP.NET on shared hosting environment [solved]

When i was trying to host my newly developed website on shared server and i stuck with following exception Security Exception in ASP.NET [SecurityException: Request failed.] System.Security.CodeAccessSecurityEngine.ThrowSecurityException(RuntimeAssembly asm, PermissionSet granted, PermissionSet refused, RuntimeMethodHandleInternal rmh, SecurityAction action, Object demand, IPermission permThatFailed) +165 System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Object assemblyOrString, PermissionSet granted, PermissionSet refused, RuntimeMethodHandleInternal rmh, SecurityAction action, Object demand, IPermission permThatFailed) +100 System.Security.CodeAccessSecurityEngine.CheckSetHelper(PermissionSet grants, PermissionSet refused, PermissionSet demands, RuntimeMethodHandleInternal rmh, Object assemblyOrString, SecurityAction action, Boolean throwException) +284 System.Security.CodeAccessSecurityEngine.CheckSetHelper(CompressedStack cs, PermissionSet grants, Per

File extension validation using javascript in MVC

This is article which demonstrate the validation of allowed file extension for upload of files in MVC using javascript. In this article i have used validation for valid image format like Jpeg, Jpg,Gif and png image format. If users ties to upload any other file apart from above mentioned file then the javascript function will shows proper message to user to select appropriate files. I have used html input file control for uploading of files in MVC application. following is the html code for it. <body> <div style="height:50px; padding-bottom:20px;"> @using (@Html.BeginForm("Create","FileUpload",FormMethod.Post,new{ enctype = "multipart/form-data"})) { <input type="file" id="file1" name="sdf" style=" margin-top:0px;" onchange="if( CheckExtention(this.value) ) {parent.ShowHideImage(true);this.form.submit();}" /> } </div> </body> and followi

Asp.Net MVC best practices part 1

If you decided to develop the application using Asp.Net MVC application and. I am going to demonstrate you to follow the best practices in MVC to develop the robust web application . If you want to log the exceptions occurs in controllers you need to write try catch blocks in each action method. This approach is also not recommended because you need to write try catch block in each action method. To get ride of this you can handle exceptions and log each exception by using centralized approach. These approaches are as follows Do not inherit the Controller base class directly to your Controllers Create new base custom controller class which inherits the base controller class Inherit created base custom controller class to your controllers Following is the sample code which demonstrate the above best practices public class BaseController : Controller { } In above code I have inherited the controller class to custom base controller class and below is the code to us