Friday, March 1, 2019

Asp.Net Core MVC Hello World Example

In the previous post we saw on how to setup a basic Asp.Net core MVC project using Visual studio. In this post we shall examine the various files and methods in the basic project. As a recap we used the following steps to create the basic MVC project.

o   Open Visual studio (I am using VS 2017) and select new Project.
o   Select Web -> .Net Core
o   Select Asp.Net Core web application
o   Provide a name (Hello MVC) and path and click OK
o   In the templates select Web Application (Model-View-Controller) and click OK
o   This will create a basic MVC project with one Controller and View

Now let us examine the files and methods in the project. Let us start with the Program.cs.
This file looks similar to the basic Asp.Net Core project file. It hooks up the host and starts up the application.

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }

Next, let us look at Startup.cs
In the Startup there are 2 main things to look out for, first we add the Mvc service in the Configure method.
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

Next we add the useMvc middleware and MVC route definitions in the Configure() method.
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

The full Startup class looks like this.

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

Build and run the application, it will load the Index view in the Home controller, which is the default route for the MVC application.


Search Flipkart Products:
Flipkart.com

No comments: