Skip to main content

Posts

Showing posts with the label MVC

Tip on MVC URL Routing

Recently, one of my colleague asked me a question, in which his routing was not working as expected. Let me mention the exact URL he was giving: URL 1:  http://localhost:port/StudentEnquiries/StudentEnquiries/44" URL 2: http://localhost:port/StudentEnquiries/StudentEnquiries/?StudentID=44  And his code snippet was as below:            public class RouteConfig       {           public static void RegisterRoutes(RouteCollection routes)           {               routes.IgnoreRoute("{resource}.axd/{*pathInfo}");               routes.MapRoute(                   name: "Default",                   url: "{controller}/{action}/{id}",                   defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }               );           }       }      Where he claimed that he didn’t change anything in his Route.Config file and his Controller looks something like this:

Consuming Services in ASP.NET Core MVC View

In continuation to my previous post entitled 'Consuming Services in ASP.NET Core Controller' on injecting services in controller, this time I'm writing on how to inject services directly in MVC View. In order to achieve this, a new keyword @inject is used. Here I'm not writing entire code again as it can be referred from my previous article. Let's register the service in ConfigureServices method as: public void ConfigureServices( IServiceCollection services) { ... ... services.AddTransient<IGUIDService,GUIDService>(); } Next is to inject service inside a View as: @inject IGUIDService guidService Now service is injected and available for use. Let's quickly use it: @ using CustomTagHelper.Services; @ inject IGUIDService guidService <p>@guidService.GenerateGUID()</p> Run your application and you will be able to see the required output as: Hope you enjoyed learnin

Consuming Services in ASP.NET Core MVC Controller

Another interesting feature of ASP.NET Core is service consumption using Dependency Injection. I already provided the overview of Dependency Injection in my earlier articles. Moving further, in this article we will see how one can inject and consume services to controller using dependency injection. In ASP.NET, you can access services at any stage of your HTTP pipeline through dependency injection. By default, it provides Constructor injection, but it can easily be replaced by any other container of your choice. Before moving ahead, one point is very important to understand and that is lifetime of services. ASP.NET Core allows you to configure four different lifetimes for your services. Transient - Created on every request Scoped - Created once per request Singleton - Created first time when they are requested Instance - Created in ConfigureServices method and behaves like a Singleton To understand the service consumption in easier way, we will create a sample applica

Features of ASP.NET Core 1.0

Exclusive Amazon Deals ASP.NET Core 1.0 is a new open-source framework for building modern Web applications. As compared to previous versions of ASP.NET there are many major changes happened. I’ll try to capture most of the changes in this blog post. ASP.NET Core 1.0 was initially named as ASP.NET 5. I’m dedicating this blog post to showcase the major features of ASP.NET Core 1.0. Single aligned web stack ASP.NET Core 1.0 is a single aligned web stack on top of ASP.NET for Web API and Web UI as shown in below figure (image taken from mva):   Above figure clearly depicts that there is very less sharing between these three stacks. If you will see from ASP.NET template view, you will notice that all the three options are dimmed out as shown below: Much leaner framework with reduced surface area ASP.NET Core 1.0 is no longer based on System.Web.dll. As per dzone , typical HTTPContext object graph takes 30K in memory while new implementation takes around 2K.I