Technology:
- Visual Studio 2017
- Asp.Net Core Tooling 1.1
- .Net Framework 4.6.2
Single Page Application's and their integration into Visual Studio have become easier, with all the new support built into Visual Studio. But earlier in the week, Jeffery T. Fritz released a really nice article on the integration and implementing with the following packages:
Microsoft.AspNetCore.SpaServices
Microsoft.AspNetCore.NodeServices
After you go through and even analyze a couple of the templates, you'll notice in your Solution Explorer, a directory called ClientApp
. This is configured, and routed via Webpack.
Inside the Startup.cs
is where the issue reveals itself.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
{
HotModuleReplacement = true
});
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
}
Inside our request, we have some routing to our MVC Framework.
The question, why do we need to specify this route? If we simply utilize the app.UseDefaultFiles()
and app.UseStaticFiles()
and have our index specified in our wwwroot. Our client side router will always be returned. So why do we not do it this way:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
{
HotModuleReplacement = true
});
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseDefaultFiles();
app.UseStaticFiles();
}
I understand that items directly in our wwwroot aren't compressed, also can leak security, but aside from those two drawbacks why not use this route to ensure your index and client side router aren't always returned?
I'm specifically asking about the concept of forcing MVC to always return a Home/Index
or UseDefaultFiles()
/ UseStaticFiles()
. What am I missing, why are we being told to force MVC to return it?
Important: Basically the issue targeted, is how to force the index to be returned so our client side framework's router is handling the changes vs the backend returning a specific view state.