Skip to main content

Posts

Showing posts with the label MVC

Making FluentValidation compatible with Swagger including Enum or fixed List support

FluentValidation is not directly compatible with Swagger API to validate models. But they do provide an interface through which we can compose Swagger validation manually. That means we look under FluentValidation validators and compose Swagger validator properties to make it compatible. More of all mapping by reading information from FluentValidation and setting it to Swagger Model Schema. These can be done on any custom validation from FluentValidation too just that proper schema property has to be available from Swagger. Custom validation from Enum/List values on FluentValidation using FluentValidation.Validators; using System.Collections.Generic; using System.Linq; using static System.String; /// <summary> /// Validator as per list of items. /// </summary> /// <seealso cref="PropertyValidator" /> public class FixedListValidator : PropertyValidator { /// <summary> /// Gets the valid items /// <...

Custom authorization based on dotnet core policy with Attribute filter

Around 2.5 years back I had written about custom authorization on MVC  Custom authorization on class, action/function, code, area level under Asp.Net MVC application , there are few approaches which are changed in Core version for authorization. Like Authorization filter approach is discouraged since it cannot be unit tested. I believe this is right step but also global or basic authentication could still be driven by Attribute due to enhancing simplicity on codes by focusing on the primary objective rather than writing authorization check everywhere. The whole approach and usage remain same from the original Post, in this, we would be just looking into making it compatible with dotnet Core MVC. You would need to go through earlier Post to understand the approach that was taken for authorization of a user. Also, can go through official post: https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies to understand new approach. More of all we need to create...

Gracefully notifying end user about application error in .Net and .Net Core versions

We generally do error logging for the application to know what error might have occurred in the application. Notifying the end user about the error is equally important. Also, it is always better to implement this at first place rather than doing at a later stage. I have seen many big company's websites ending with .NET yellow page of death. Now, it is really good that the default behavior is removed from .Net Core framework with some generic error message. We would look how to implement generically and use in older frameworks and some tips on .Net Core as well. The idea is to create MVC Controller for error and then redirect it from any locations like Filters or Global Exception handling. Also, we would briefly look into the global AJAX error handling system since heavy usage of AJAX in current development trend. The few steps that we are going to do. - Represent application errors with enum as system/application have different ways to deal with errors like though Exceptio...

LDAP with ASP.Net Identity Core in MVC with project.json

Lightweight Directory Access Protocol (LDAP), the name itself explain it. An application protocol used over an IP network to access the distributed directory information service. The first and foremost thing is to add references for consuming LDAP. This has to be done by adding reference from Global Assembly Cache (GAC) into project.json "frameworks": { "net461": { "frameworkAssemblies": { "System.DirectoryServices": "4.0.0.0", "System.DirectoryServices.AccountManagement": "4.0.0.0" } } }, These  System.DirectoryServices  and  System.DirectoryServices.AccountManagement  references are used to consume LDAP functionality. It is always better to have an abstraction for irrelevant items in consuming part. For an example, the application does not need to know about PrincipalContext or any other dependent items from those two references to make it extensible. So, we can begin wi...

Client side Validation for data driven view engine

The prerequisite for this is to have a designed database driven view engine. This can be a good guidance to implement DB driven view engine Data Driven Custom View Engine in ASP.NET MVC (http://www.dotnetcurry.com/aspnet-mvc/946/data-driven-custom-view-engine-aspnet-mvc) . If we talk about the concept then we can say for DB driven view engine a dynamic form/screen table would require along with the associate attribute set for controls. The controls, Attribute set can have constraints like Required, MaxLength, RegEx etc. similar to available DataAnnotation implementation, just that it has to come through DB. The jQuery unobtrusive validation is all about adding certain HTML 5 data attributes. So, if we can find rules of the controls (required, max length etc) and set it to HTML attribute from view engine then we are done. While designing DB driven view engine, there should be a place where we need to loop through available controls to identify it's type and write as HTML/elem...

Consuming built MVC project under other MVC app

The idea is to reuse build application on multiple child application as a lot of Views and Codes need to be same on all children application. This approach can be a good candidate for a multitenant application, some sort of pluggable architecture or reusing MVC app as parent project. There are three things need to be done to achieve. Project built event to copy Views from the parent application. Area registration on child application. Custom View path through customized Razor view engine. Project built event to copy Views from the parent application. This has to be configured on child application to copy from application build event. It can be done by going into project property => Build Events => Edit Post Built (Post-built event command line). Ex: xcopy "$(SolutionDir)"..\“App.Common\App.Common.Web\Views” “$(ProjectDir)\CommonAppLoc\Views\” /s /i /y /f xcopy "$(SolutionDir)"..\“App.common\App.Common\App.Common.Web\Areas” “$(ProjectDir)\CommonA...

File upload mechanism to support Local server, File Server and Azure server

Making direct dependency with Azure sounds as a big problem if there is a possibility of switching to other platforms in future. Also, in general, making dependencies with Azure also makes mandatory to run the application through Azure Emulator which itself takes the time to load application whenever need to be debugged. It takes huge amount of development time also based on architecture point it is not a good idea to have any hard dependency in application. In this article, we will look into creating a mechanism for file upload which could be switched from Azure to local server or vice-versa. The two key parts that are needed are to access the uploaded file and to upload the file. The starting stage is to create interfaces to access file path and upload files to server. This would allow us to change the implementation based on need. Interfaces IFileUpload : Interface for uploading files using MyProject.Model.DTO.Upload; using MyProject.Model.Enumeration; ...