Monday, October 15, 2018

.Net Core | Start with MVC

Using MVC in .Net Core
  1. Register MVC as middleware in http Request Pipeline
    app.UseMvc()
    You can also configure Routing with registration of MVC as middleware.           

    Below method can be used if your controller is Home, action in that is Index.
     app.UseMvcWithDefaultRoute()
    If you have different name controller/action than you can configure route as given below
    Id is optional parameter
     app.UseMvc(routes => 
                  routes.MapRoute(
                        name: "default",                   
                        template: "{controller=EquityHome}/{action=Login}/{Id?}") 
                   )
  2.   
  3. Inject MVC in IOC Container
     services.AddMvc()
    You can set .Net Core version as well with injecting MVC using SetCompatibilityVersion method.
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)


  4. Configure HttpRequest pipeline with some important and required features/services
    All these services have IApplicationBuilder parameter and returns it back IApplicationBuilder after completion of operation (we no need to mention it)
    • app.UseHttpsRedirection()
      Enables security by redirecting all http requests to https
    • app.UseStaticFiles()
      • UseStaticFiles(IApplicationBuilder) Enables static file serving for the current request path
      • UseStaticFiles(IApplicationBuilder, StaticFileOptions) Enables static file serving with the given options
      • UseStaticFiles(IApplicationBuilder, String)
        Enables static file serving for the given request path

    • app.UseCookiePolicy()
      Different cookie policy options can be set with CookiePolicyOptions parameter
    • app.UseStatusCodePagesWithRedirects("~/errors/{0}.html")
      Can be used to redirect to custom error page, where parameter {0} is error status code
    • app.UseStatusCodePagesWithReExecute("~/errors/{0}.html")
      Can be used to redirect to custom error page, where parameter {0} is error status code.
      This method re-executes http request pipe line with given url 
    • app.UseDeveloperExceptionPage()
    • This method display error details on web page.(Yellow error page)
  5. Implement security using, It enforces browsers to use https
    app.UseHsts()

  6. MVC Application architecture with .Net Core   

  1. Microsoft.AspNetCore.App provide all .net core features.
  2. site.css is java script is empty java script file for development and site.min.css is for other environments.
  3. site.js is java script is empty java script file for development and site.min.js is for other environments.
  4. 'bootstrap' folder contains bootstrap library files. 
  5. 'Controller' folder contains controller cs files same as traditional mvc structure.
  6. Model/ErrorViewModel.cs is model for custom error page Shared/Error.cshtml
  7. _CookieConsentPartial.cshtml is used to take user confirmation to configure cookie policy. This partial view contains 2 buttons 
    1.  Learn More: This button redirects to view Privacy.cshtml where you can mention privacy policy of your site.
    2. Accept: By clicking this button user accepts CookieConsent, it adds AspNet.Consent cookie with value true. This means you are allowing application to create and send non-essential cookies to the browsers.
      If you do not accept it only essential cookies would be sent to browser.

  8. _Layout.cshtml is the master view that displays with every view.
  9. _ValidationScriptsPartial.cshtml contains reference to java script files used for validation.
    It contains references for 2 java script files for development and non development environments. For Dev environment it has local project path of files, and for non Dev environments like Stg and Prod it has web path of these files.
     asp-fallback-src: It has local project path of files in case of failure to load from web path (For non Dev environments).

     
  10. Shared/Error.cshtml is custom error page display's on non development environments, model of this view is Model/ErrorViewModel.cs.  
  11. _ViewImports.cshtml loaded for all views before loading any view, so its used to import namespaces which are to be used by every view. So, rather than adding same reference on top of each and every View, it can be moved to _viewImports.cshtml. By default, this file contains the below lines of code

    TagHelpers is the namespace used almost in every view so it get imported in this file.
  12.  _ViewStart.cshtml render for every view by default (its like master page in classic asp.net), so its used to add common UI that you want for each and every view, instead of adding the same in each view. By default, this file contains the below lines of code

  13. In classic aps.net we used to keep key-value pairs required for configuration in web.config. Now in .net core ini, json or xml files can be used to store key-value pairs used for configuration.
    appsetting.json is the json configuration file to store key-value pairs.
    appsetting.Development.json is the json configuration file to store key-value pairs for dev environment.
    Note: You can have multiple environment specific appsettings.<Environment>.json files.
    appsetting.json loaded first than environment specific file loaded and overwrites it.

  14. Program.cs is the startup file for the web application. Runtime calls main method of this class. Main method creates WebHostBuilder using startup class.
  15. Startup.cs is the class that implement dependency injection and middlewares in httprequest pipeline. 

No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...