MVC 6 with vNext: Do we still need the Global.asax? - asp.net-mvc

I'm developing an application using MVC6.
I noticed that the Global.asax file disappeared by default there is startup.cs file that calls the config. My question is how do I grab the Application_Start event method ?
Do I still need the Global.asax ?
Why has it been removed by default ?

Global.asax is only present when there is a reason to hook into it. The default MVC6 project has no hooks into it, therefore it doesn't provide one. Just add new item, global.asax
I should also suggest, that I would avoid creating a global.asax unless you need absolutely need it. People tend to reach for Application_Start or Session_Start first when there are probably better places to do what you need to do. Consider creating an OWIN module, or consider adding your own Startup module.

Related

How NopCommerce loads admin area

The question is so simple. How NopCommerce web project (Nop.Web) loads the Admin area while it's separately in another DLL placed in root projects bin dir?
I have worked around AutoFac but seems it's not related to this.
Open Global.asax.cs from Nop.Web and then see Application_Start in method and you can see a line there
AreaRegistration.RegisterAllAreas();
By this line it registers all area of your domain.
The RegisterAllAreas method finds all types in the application domain that derive from AreaRegistration and calls each of their RegisterArea methods.
For more info check this link.

What is the purpose of IApplicationBuilder.New()

In the new ASP.NET 5.0 (vNext), the startup code relies on the IApplicationBuilder interface. The Use method is used to add a handler to the builder, while Build is used to construct the final delegate. But I can't figure out what is the purpose of New. I've been digging in GitHub, but can't find any place where that's used.
Anyone understand what is the purpose of that method?
New() creates a second ApplicationBuilder, sharing all the ApplicationServices and ServerFeatures of the first one, but none of the middleware. It is used internally by the branching extensions (Map, MapWhen, UseWhen) to create the new 'branch'.
You can find the implementation here: ApplicationBuilder.cs.
In some cases, it is also useful in higher-level frameworks.
For exemple, the [MiddlewareFilter] attribute in MVC Core uses New() internally to execute a piece of ASP.NET Core middleware inside the MVC framework (i.e. as a filter). MVC Core creates a small pipeline around the middleware, builds it into a RequestDelegate, then runs the HttpContext through it. Just like ASP.NET Core does with your 'main' pipeline built in Startup.cs.
Thanks to this feature, we can reuse a piece of general-purpose ASP.NET Core middleware, from inside MVC.
For more information, see MiddlewareFilterBuilder.cs in ASP.NET MVC Core.
It appears to be there to branch [clone] the original instance (as can be demonstrated in src/Microsoft.AspNet.Http/Extensions/MapExtensions.cs). There was also a previous MapWhenExtensions.cs, but it appears to have been removed from the dev branch.)
I suspect it's an artifact of a previous design that would provide the ability to bind middleware based on circumstances without affecting the root's configuration. The fact that it's been there since before IBuilder was refactored to IApplicationBuilder and that most dependencies were in files that have since been removed from the dev branch, I would venture a guess that it's old news.
Of course it's hard to tell given neither the interface nor the base implementation are commented.

Get URL to website from within Application_Start?

Hi,
I need to generate a correct URL to a action in my APS.NET MVC 3 website and this have to be done in the Application_Start in Gloabl.asax.
I have tried to set a UrlHelper but the HttpContext.Current is always null?
The adress i look for is somthing like this : http://www.mysite.se/MyController/MyAction.
I have also tried VirtualPathUtility.ToAbsolute("~/") but that will only give me "/mysite/"
I'm afraid you can't access the HttpContext in the Application_Start when the application is running in the IIS integration mode (you can achieve that in the classic mode but that's not recommended).
Mike describes here a workaround how to do that in Application_BeginRequest such that your code still runs only for the first request not for the later ones.

What does WebActivator do?

This code was generated for me after added entity framework code-first for SQL Server CE using NuGet. They did no changes to any other file. The file SQLCEEntityFramework.cs was created and placed in App_Start folder.
Does this mean it automatically gets executed or something? The same thing happened when I added Ninject for MVC 3. No code was added to the global.ascx file so I have no idea if its plug and play or I have to configure something.
[assembly: WebActivator.PreApplicationStartMethod(typeof(StackTorrents.WebUI.App_Start.SQLCEEntityFramework), "Start")]
According to:
http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx
This new attribute allows you to have
code run way early in the ASP.NET
pipeline as an application starts up.
I mean way early, even before
Application_Start. This happens to
also be before code in your App_code
folder (assuming you have any code in
there) has been compiled. To use this
attribute, create a class library and
add this attribute as an assembly
level attribute. A common place to add
this would be in the AssemblyInfo.cs
class within the Properties folder.
To clarify, it gives you a way of hooking into several application start and application shutdown events WITHOUT having to change any existing code files (previously you had to edit Globals.asax.cs).
This is mostly a big deal when making packages as these events are really useful for bootstrapping Http modules and it is really difficult to write code into existing files.

application_start event called multiple times in MVC application

In my MVC application ,I am updating my web.config at runtime through application_start event.So, ideally it should be done only when the application is started.BUT in MY mvc application the application_start event of global.asax is being called multiple times , even when i have not restarted the application.
Its being repetadly called when i am calling different actions , so the webconfig is repetedly updating & making my application very very slow.
Can you please let me know , what's the reason & how to handle this .
Thanks in advance
Aayushi
Everytime you change something in your web.config. That will refresh your application, therefore application_start is also called everytime you open your site. When web.config is changed, your application restarts.
I dont know which elements in your web.config you are updating. If these are custom elements maybe you can put these things in a separate config file (xml file) and update that file.
You application will be restarted once web.config is modified.
It doesn't make any sense changing web configuration file in application_start. Moving changeable part into a separate file

Resources