MVC4 Razor URL resolution do not work - asp.net-mvc-4

I have upgraded my project from VS2010 MVC3 to VS2012 MVC4 recently and found out that new Razor URL resolution feature do not work do not work.
For example I have fav icon
and ~/ path is not automatically resolved by Razor. If I use old style(MVC3) Url.Content resolver everything works fine.
If I create new MVC4 project then paths are resolved successfully. I think that something is left not enabled when VS2012 upgraded my project but can't find what. Any ideas?

I have found teh problem by myself. It seems the project was upgraded that it still use MVC3, the project MVC version was not changed automatically.

Related

Hot Reload not working in Visual Studio 2022 .Net 6 Razor projects

We just upgraded all but one of our web app projects from .NET Core 3.1 projects to .NET 6 in our solution. These were ASP.NET Core 3.1 projects using Razor. Here are the results we're seeing with respect to Hot Reload:
The .NET 6 projects hot reload doesn't seem to work. Hot Reload is enabled, but our Front End is never updated. Refreshing the page doesn't seem to show the updates either.
The ASP.NET Core 3.1 project hot reload is working as expected.
When we run these projects in VS 2019, we can refresh the pages in any of our projects and we will see the updates. Are there some settings that we're missing here?
Big Thank you to Guru Stron! His comment in my question directed me to where I should've looked. We had to:
Update our Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation Nuget package
Add this to our launchsettings.json: ASPNETCORE_HOSTINGSTARTUPASSEMBLIES to "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
This answer solved our issue - .NET Core MVC Page Not Refreshing After Changes
First, Make sure all options are selected
In Hot reload, You can select Hot Reload On File Save(Not selected by default), Then VS will hot reload after you ctrl+s;
Demo
I had the same issue and after trying everything including rolling back the version and adding .AddRazorRuntimeCompilation() to Program.cs still didn't work. What I did was literally re-check the "Hot reload on file save" option when you click on the down arrow to bring up the menu on the hot reload button from the menu bar. That did the trick for me.
Hopefully this works for you.

Version conflict occur in Microsoft.Entity.FrameworkCore

I am working on a .net core web application with razor pages.
My application structure is like :
DemoCoreApplication with class Libraries "BLL.Data" and "BLL".
In BLL.Data, I have made the DB context then I have added the reference of "BLL. Data" project into my main project i.e: DemoCoreApplication.
Refrences in BLL.Data class library:
After building the main project (DemoCoreApplication), I got some strange version conflicting errors.
I believe the following steps will solve your problem
Install .Net Core 2.2 SDK (if you don't have)
Convert all projects in the solution to use .Net Core 2.2
Rebuild, Start the project

.NET Core MVC Page Not Refreshing After Changes

I'm building a .NET Core MVC on the latest version 2.2. I have a problem when I make changes to the CSHTML file and refresh the page, my changes are not reflected in the browser. I have to restart the project in order to see my changes. This has been happening for a while now so I'm not exactly sure what change caused this issue.
I've tried using the chrome's "Empty Cache and Hard Reload" as well as other browsers to no avail. This happens on Windows and Mac using both Visual Studio for Mac and VS Code
In a default .Net Core project it works fine so it must be something in my project that changed along the way. I'm wondering where I need to start in order to debug this issue? I've tried commenting out almost everything in my Startup.csand Program.cs with no resolution.
In ASP.NET Core 3.0 and higher, RazorViewEngineOptions.AllowRecompilingViewsOnFileChange is not available.
Surprised that refreshing a view while the app is running did not work, I discovered the following solution:
Add Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package to the
project
Add the following in Startup.cs:
services.AddControllersWithViews().AddRazorRuntimeCompilation();
Here's the full explanation for the curious.
There was a change made in ASP.NET Core 2.2 it seems (and I can't find any announcements about this change). If you are not explicitly running in the 'Development' environment then the Razor Views are compiled and you will not see any changes made to the .cshtml
You can however turn off this using some config in your Startup class as follows.
services.AddMvc().AddRazorOptions(options => options.AllowRecompilingViewsOnFileChange = true);
For ASP.NET Core 3.0 and higher, see Alexander Christov's answer.
I've just created a new project using the latest ASP.NET MVC Core 3.1 template and I altered the following to get runtime recompilation enabled for Debug:
Reference NuGet package - Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.
Startup.cs - ConfigureServices(IServiceCollection services) WAS:
// stuff...
services.AddControllersWithViews();
// more stuff...
NOW:
// stuff...
var mvcBuilder = services.AddControllersWithViews();
#if DEBUG
mvcBuilder.AddRazorRuntimeCompilation();
#endif
// more stuff...
In addition to Alexander Christov's answer, since ASP.NET Core 3.1 you can enable the view compilation for development environment without changes to the Startup file:
Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
Set next environment variables (for example via environmentVariables section in launchSettings.json):
ASPNETCORE_ENVIRONMENT to "Development".
ASPNETCORE_HOSTINGSTARTUPASSEMBLIES to "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation".
You should just add this:
services.AddControllersWithViews();
to the ConfigureService method.
Be aware below code is not available in ASP.NET Core 3.1:
services.AddControllersWithViews().AddRazorRuntimeCompilation();
For those using Net core 3.0 or greater
Go to Tools → Nuget package manager → Manage nuget pakages for solution
move to browse tab to browse from internet
search for RuntimeCompilation
Click on Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
install it on your intended projects the current stable version
open Startup.cs file
go to void method ConfigureServices
add line: services.AddControllersWithViews().AddRazorRuntimeCompilation();
you are DONE
Rerun and see. Now you can refresh your views or pages.
first of all install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation using nuget manager
after that add below code into your startup.cs
services.AddRazorPages().AddRazorRuntimeCompilation();
net6.0 also this works.
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
Using .net core 2.2 running app with command dotnet watch run the project is restarted after every change
Below helped me when views were in separate project.
if(HostingEnvironment.IsDevelopment()){ // only in development (optional)
services.AddMvc().AddRazorOptions(o => {
o.FileProviders.Add(new PhysicalFileProvider(PATH_TO_PROJECT));
});
}
I had the same issue while working on a .NET 6 MVC Web App.
I installed Microsoft.AspNetCore.Mvc.Razor.Runtime.Compilation from NuGet Package Manger then added .AddRazorRuntimeCompilation(); after
builder.services.AddControllersWithViews();
so that it looks like this
builder.services.AddControllersWithViews().AddRazorRuntimeCompilation();
and it worked!
Hope this helped.
I was able to solve this problem in Rider by adding the ASPNETCORE_ENVIRONMENT=Development environment variable.
There are two ways to resolve this issue:
1. Check the permissions of folder in which your .sln file present.There may be a problem with file access permissions as Visual studio may not be to access files when IIS express server is running, so to reflect new .cshtml changes each time you need to restart the server,so I suggest edit the folder access permissions by:
Right click on folder->properties->security->click on edit button -> check all options->save.
Restart Visual studio to see changes.
If this does not work then use 2 option.
2.In your project in startup.cs file add this below line ConfigureServices() in method :
services.AddMvc().AddRazorOptions(options => options.AllowRecompilingViewsOnFileChange = true);
Are you absolutely sure you are using 2.2? Check your csproj because it might be this bug https://github.com/aspnet/Razor/issues/2466
You could try turning off RazorCompileOnBuild more info https://learn.microsoft.com/en-us/aspnet/core/razor-pages/sdk?view=aspnetcore-2.1#properties
I had a similar problem upgrading from .net Core 3 to .net 5.0
Problem was due to old dependency in Telerik controls that we could not change.
Fixed by changing references in the .csproj file
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="3.8.0" />
to
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.8.0" />
(your version may be different)
In Visual Studio 2022 Preview, it seems there is an option called Hot Reload for this purpose.
It seems to be available in Visual Studio 2019 too.
With Hot Reload you can now modify your apps managed source code while
the application is running, without the need to manually pause or hit
a breakpoint. Simply make a supported change while your app is running
and in our new Visual Studio experience use the “apply code changes”
button to apply your edits.
https://devblogs.microsoft.com/dotnet/introducing-net-hot-reload/

How to add a compatible xunit Project to an asp.net core solution?

I have a somewhat weird problem:
I have installed Visual Studio 2015 and 2017 RC on my system, as well as Asp.Net Core 1.0 and 1.1
I have build a new Solution, containing a new Asp.net Core 1.1 Project - called Upload
Now I want to add an Xunit Test Project, which can be done either from the CLI or via menu, I tried both.
This project has the following dependencies:
But now the whole solution fails to compile, b/c Xunit seems to be netcore 1.0, whereas my project is 1.1?!?
How can I solve this? I am unsure if I have installed something wrong, or if there is another rational explanation.
After a lot of digging into GitHub issues and other SO posts I finally fixed the issue by following the direction of BalassaMartin here.
TL;DR: install the Microsoft.NET.Test.Sdk NuGet first.
My guess is that xunit.runners.visualstudio references a previous version of Microsoft.TestPlatform.ObjectModel that does not target .NET Core. Adding the Microsoft.NET.Test.Sdk
package solves the problem because it references the 15.0.0 version.
After doing this the xunit.runner.visualstudio package installed fine. However now I'm back to the classic problem of not being able to actually see my tests in the explorer because VS can't find them.
I'll update when solved if the visibility issue is relevant to OP
Update
The reasons I still could not find tests was a dotnet core version mixup. Its likely to happen to a lot of people. See SO post for solution.

Creating NuGet Packages - Why doesn't this folder structure work for .NET 4.0?

Here is the issue I'm having - I cannot get my project's NuGet package to work for .NET 4.0.
Here is what my folder structure looks like:
lib\
\.NET 4.0
binary.dll
\Silverlight 4.0
binary.silverlight.dll
Whenever I try to add this package to a Silverlight 4 project, it works just fine. When I try to add it to an ASP.NET MVC3 app I get the following error: "unable to find assembly references that are compatible with the target framework '.NETFramework, version=v4.0'"
What am I doing wrong?
Change the folder name to Net40 and SL4 (or Silverlight4 no spaces). You should file a bug though, those names should work. I think the "." in .NET might be throwing it off.