wwwroot serving pages not MVC6 - asp.net-mvc

I am currently working on an Asp.Net 5 / mvc 6 app. I was running the beta5 release and updated to beta7. I have noticed my index page is loading from the wwwroot directory (I started the app with an index page in the wwwroot and am now using mvc, making the index in the wwwroot redundant)
All of my mvc views were loading correctly prior to the update to beta7, I ideally do not want to go back to beta5.
I have included mvc in the Startup.cs
app.UseMvc(config =>
{
config.MapRoute(
name: "Default",
template: "{controller}/{action}/{id?}",//optional id
defaults: new { controller = "App", action = "Index" }
);
});
Project.json
"dependencies": {
"Microsoft.AspNet.Mvc": "6.0.0-beta7",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta7",
"Microsoft.AspNet.Server.IIS": "1.0.0-beta7",
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta7",
"Microsoft.AspNet.StaticFiles": "1.0.0-beta7",
"Microsoft.Framework.Configuration.FileExtensions": "1.0.0-beta8",
"Microsoft.Framework.Configuration.Json": "1.0.0-beta7"
},

So after upgrading to beta8 and running dnvm upgrade this has resolved my issue. I believe my configuration was not entirely in line. Thanks to meligy & juergen-gutsch for their answers, very useful to know.

In your Startup class ("Startup.cs" file under src/project-name), in the Configure() method, this line:
app.UseStaticFiles();
probably comes before:
app.UseMvc(routes => ... );
You can change the order so that UseMvc(...) comes before UseStaticFiles(...), but note that this means MVC will try to handle all requests to images etc as well even if it doesn't need to.
You can also just change-the-name-of / move / remove the index file now that don't need it anyway.

Create a complete new ASP.NET 5 project and compare the project.json and the startup.cs with that files from your updated project.
This is the best option to see what is wrong with your current configuration.

Related

Web optimizer .net core

Upgrading .net 4.8 mvc app to .net 6. I am keeping the static content in the same folders as in the previous web app project and using the web optimizer to minify and bundle.
I am seeing lot of console errors and the UI is not loading for the app
Layout cshtml
Script and Content folder
It may help if you could show how you configured in program.cs
I tried as the document :
In program.cs:
builder.Services.AddWebOptimizer(x =>x.AddJavaScriptBundle("/js/bundle.js", "js/chat/*.js"));
call
app.UseWebOptimizer();
before:
app.UseStaticFiles();
in chat.js:
console.log("chat");
in hellow.js:
console.log("hellow");
And my project:
in my View,I called:
<script src="/js/bundle.js"></script>
The result:
other document may help:
Update:
I tried to add two floders under the directory of the project as your comment and tried as below to fix the 404 error:
var provider1 = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "Script1"));
var provider2 = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "Script2"));
builder.Services.AddWebOptimizer
(x =>
{    
x.AddJavaScriptBundle("/bundle1.js","/chat/*.js").UseFileProvider(provider1);
   x.AddJavaScriptBundle("/bundle2.js","/comment/*.js").UseFileProvider(provider2);
});
My project:

How to remove ErrorViewModel when using dotnet core with MVC

If ErrorViewModel is removed from my project, it won't run, failing at app.UseMvc() with the error:
System.TypeLoadException: 'Could not load type
'DocumentGenerationService.Models.ErrorViewModel' from assembly 'DocumentGenerationService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.'
It is a dotnet core 2.2 application with mvc. I've removed everything that is unnecessary to the application, which includes all the views, as this is a webapi project.
Why can't I remove ErrorViewModel from the project? It's making me sad.
For reference, here's how the startup class looks:
public class Startup
{
...
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddSignalR();
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseMvc();
}
}
Any ideas how to get rid of the ErrorViewModel class and have the project run? Or an explanation of why it isn't possible?
Thanks StackOverflowers!
I had the same problem and the following solution worked for me:
Remove all the ErrorViewModel related files like Controllers, Views, etc...
Close the Visual Studio if it's open
Go to the source directory and remove obj and bin directories
Open the project with Visual Studio and run it
Now it should work as a normal WebApi project.
First welcome to the Stack Overflow community.
Now after removing the ErrorViewModel from models folder you need to do following things to remove the ErrorViewModel related codes:
Delete the Error method from HomeController
Delete Error.cshtml file from Views/Shared folder
Now check _ViewImports file in the Views folder. If there is any red line starting with #using then please remove this line.
Now build the solution and run again. Hope it will run now gracefully.

All angularJs templateUrl paths invalid

So I'm writing a small app and currently working on the angular js front end. The backend is written in RoR but thats besides the point. I have a dependency ui.router in my angular.module so I can move around pages.
Link to github branch if interested: linky link
Paths:
App
-Assets
-Javascripts
-app.js (where the routing lies)
-Templates
-dashboard.html (this is the template I want to render)
-Views
-Layouts
-Index.html.erb <- this is the index
Heres the problem:
angular.module('SwoleMetrics', [
'ui.router',
'templates'
])
.config(function ($stateProvider, $urlRouterProvider, $locationProvider) {
/**
* Routes and States
*/
$stateProvider
.state('dashboard', {
url: '/dashboard',
templateUrl: 'dashboard.html', <<--- RIGHT HERE
controller: 'DashboardCtrl'
});
// default fall back route
$urlRouterProvider.otherwise('/dashboard');
// enable HTML5 Mode for SEO
$locationProvider.html5Mode(true);
});
No matter what path I put into templateUrl, it never works. I have the same html file in basically every folder around app.js to see if it could read any of those but it always fails and just recursively inserts the parent div into the . Do any front end engineers know why?
The issue was the way the files were being served via rails. The sprockets gem was updated and no longer compatible with angular-ui-templates and broke everything. Downgraded to sprockets v. 2.x and it worked.
Its going to be relative to index.html. If index.html is under App then your path is going to be templateUrl: 'Templates/dashboard.html'.

Telling ASP.NET MVC 4 not to route static images - CSS, JS, ICO and ZIP files

I thought I'd got all my routing sorted! Just one little glitch to sort out, but first I need to explain our set-up.
I decided against a catch-all router, and instead am trapping HTTP errors in my web.config file (this question helped). First I turned the old-fashioned CustomErrors off:
<!--<customErrors mode="Off" />-->
Then I turned HTTP error-trapping on:
(as often happens, I couldn't seem to insert this as script). In the interests of full disclosure, my web.config file includes this:
I'm not trapping any application errors in Global.asax.cs. This all works fine - I then have a router which picks up on 404 errors:
routes.MapRoute(
"Error 404",
"Error/MissingPage404",
new { controller = "Error", action = "MissingPage404" }
);
and another for 500 errors:
routes.MapRoute(
"Error 500",
"Error/ServerError500",
new { controller = "Error", action = "ServerError500" }
);
My question is: how can I stop static files being trapped by this? I've already solved the problem for images, thanks to this question, which is to include these lines at the top of my routing config file:
routes.IgnoreRoute("{*allfiles}", new { allfiles = #".*\.(gif|jpg|png|ico)" });
However, the equivalent doesn't work for .js, .ico, .css or .zip files. I tried from another site:
routes.IgnoreRoute("{*allaspx}", new { allaspx = #".*\.css(/.*)?" });
routes.IgnoreRoute("{*allaspx}", new { allaspx = #".*\.ico(/.*)?" });
but that did nothing either. Again in the interests of disclosure, I've got this line at the top of the routing config file, but my understanding is that this only affects files which are found:
routes.RouteExistingFiles = true;
Can anyone help? It seems like MVC is brilliantly thought out, right up until the point of making routing easy to understand and implement.
Many thanks in advance
Andy

VS2013 Browser Link "The controller for path '/9ac086a69364466a841e03e001f946fd/arterySignalR/ping' could not be found."

Since updating to VS2013, we receive this error when running our (MCV4) web app:
The controller for path '/9ac086a69364466a841e03e001f946fd/arterySignalR/ping' could not be found.
I know that it relates to browser link although i'm not sure what we need to do to make it work correctly. Is there some configuration change we need to make to support this new feature?
I disabled browser link. Second #4 at this link.
http://blogs.msdn.com/b/webdev/archive/2013/06/28/browser-link-feature-in-visual-studio-preview-2013.aspx
If you would like the benefit of Browser Link but don't want the missing controller path exceptions, you can add an ignore regex to your route collection. This is what I did:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
#if DEBUG
routes.IgnoreRoute("{*browserlink}", new { browserlink = #".*/arterySignalR/ping" });
#endif
//...
}
The regex technique is courtesy of this Phil Haack post.
On VS2013 #Todd's solution didn't work for me, so I made my own.
Hope it saves you some time.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
#if DEBUG
routes.IgnoreRoute("{*browserlink}", new { browserlink = #".*__browserLink.*" });
#endif
}
Add the following to your root web.config:
<appSettings>
<add key="vs:EnableBrowserLink" value="false" />
</appSettings>
This happens to be a known issue with SignalR and has been fixed in SignalR 2.0.1 and 1.1.5:
2.0.1: https://github.com/SignalR/SignalR/issues/2569 (not yet released)
1.1.5: https://github.com/SignalR/SignalR/issues/2570 (not yet released)
Long story short, nothing you can do to change it, should just wait for the next release of browser link which has a newer version of SignalR.
Had in VS2013, after some project nuget package updates.
Cleaned the solution, closed VS and IISExpress from try and resolved

Resources