I have updated my ASP.NET 5 project to beta 8, and we are now supposed to the following web command
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
},
Now i have updated my project with the environment variables.
This has also updated my launchSettings.json file, as so
{
"profiles": {
"web": {
"commandName": "web",
"environmentVariables": {
"ASPNET_ENV": "Development"
}
}
}
}
But for some reason, every time I run the command dnx web it says that the hosting environment is Production. Why is it not starting in development mode?
The settings in launchSettings.json are only used by VS. If you run from a console, you have to set that environment variable manually.
CMD:
set ASPNET_ENV=Development
dnx web
PS:
$env:ASPNET_ENV=Development
dnx web
Adding to #Victor Hurdugaci answer, you could also avoid "messing" with current environment by passing needed variables on command line.
Inside project.json file, say that you have a web-dev command specific for development environment:
"commands": {
"web-dev": "Microsoft.AspNet.Server.Kestrel
--ASPNET_ENV Development --Hosting:Environment Development
--config hosting.Development.json",
},
where you can see how both ASPNET_ENV, Hosting:Environment are set, as well as invoking a specific hosting.json configuration.
NOTE: command is split on several lines just for readability, join again before actually pasting in JSON file.
The command: set ASPNET_ENV=Development is now obsolete instead you can use CMD:
set ASPNETCORE_ENVIRONMENT=Development
Related
I cannot get VS Code to build an empty class library while dotnet core can quite happily.
In PowerShell I create a folder called CoreTesting, navigate into it and launch VS Code with code.
I hit CTRL + ` to enter the terminal and navigate into the solution's folder.
I then enter dotnet new classlib --name Common, see the new folder Common and enter dotnet build .\Common\ to build the class library. All is well.
I add the Common folder to VS Code and hit CTRL + SHIFT + B and see No build task to run found. Configure Build Task..., so I hit return and see Create tasks.json file from template, so I hit return again and see:
MSBuild
maven
.NET Core
Others
So I select .NET Core and see that a .vscode folder is created containing tasks.json. This file contains:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet build",
"type": "shell",
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
I hit CTRL + SHIFT + B again and see the option build Common, so I hit return and see this:
> Executing task in folder Common: dotnet build <
Microsoft (R) Build Engine version 16.0.225-preview+g5ebeba52a1 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
The structure I can see is this:
Common\
.vscode\
tasks.json
bin\
obj\
Class1.cs
Common.csproj
What have I done wrong?
I was able to reproduce your problem on v1.41.1 for Windows. In doing so it created this tasks.json which is similar to yours...
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "shell",
"args": [
"build",
// Ask dotnet build to generate full paths for file names.
"/property:GenerateFullPaths=true",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
When you invoke a task, it defaults to using the workspace folder (CoreTesting) path as the working directory. However, the project file is a directory beneath in Common, hence the error The current working directory does not contain a project or solution file.
A quick fix for this is to simply open the directory with the project file as the workspace folder (i.e. File → Open Folder... → Select the Common directory).
Alternatively, if that solution is undesirable then with CoreTesting opened as the workspace folder you can configure the task to execute with a different working directory. To do this, set the task's options.cwd property...
{
/* snip */
"tasks": [
{
/* snip */
"problemMatcher": "$msCompile",
"options": {
"cwd": "${workspaceFolder}/Common"
}
}
]
}
I found this property in the Schema for tasks.json, and it's also mentioned in the Custom tasks section of the Tasks documentation. After making either change above the library builds successfully for me.
I want to start my app from the command line and automatically start a browser and show a default page (as it is run from IDE). I tried with this command:
dotnet run --launch-profile "MyApp"
In My app launchSettings.json I have defined:
"MyApp": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:53348/"
}
But the browser does not start.
Basically, in this case it is possible to run two programs in a batch file one after another, and this would solve a case.
In PowerShell, you can combine the two commands into a single line:
dotnet run --launch-profile MyApp | start chrome http://localhost:5000
I used to use asp.net mvc4 and in IIS my website's physical path would point my solution directory, and every time I update my code, I just re-build my solution and then I can use "Attach to process" (w3wp) to start debugging.
In asp.net core, when I publish my website to file system, I can run my website using IIS with no-managed code. But when I point my IIS Website to my solution code of website, it shows 502 error.
You don't need to run .Net Core in IIS to get easy debugging etc like we used to do as you described.
With .Net Core you can just open a command line at your project root and type "dotnet run"
DotNet Run uses environment variables to drive what it does. So if you want your site to run on a specific URL or port you Type:
SET ASPNETCORE_URLS=http://example.com
Or if you just want it to run on a different port
SET ASPNETCORE_URLS=http://localhost:8080
Then to set the Environment
SET ASPNETCORE_ENVIRONMENT=Development
Once all your environment variables are set, you type
dotnet run
Now to debug it, you attach to cmd.exe with dotnet run in it's Title. You'll be able to debug your code that way.
Now, if you are using Visual Studio There is a file called "launchSettings.JSON" under Properties in your project. You can configure profiles here and I have my default profiles set to Kestrel Development and then Kestrel Production, with IIS dead last so that I don't F5 run in IIS Express.
My LaunchSettings.json looks like this:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:56545/",
"sslPort": 0
}
},
"profiles": {
"Kestrel Development": {
"executablePath": "dotnet run",
"commandName": "Project",
"commandLineArgs": "dotnet run",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "http://localhost:8080"
}
},
"Kestrel Production": {
"commandLineArgs": "dotnet run",
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_URLS": "http://localhost:8080",
"ASPNETCORE_ENVIRONMENT": "Production"
},
"executablePath": "dotnet"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
The first Profile is what F5 uses when you press it. So when I press F5 Visual Studio launches dotnet run for me and set's the Environment and URLS as specified by the environmentVariables section of my profile in launchSettings.JSON.
Now because I have multiple Profiles I get a drop down next to the run button so I can select Kestrel Production if I want to run in Production mode locally.
Follow these steps to be able to achieve what you want.
In launchSettings.json, add a property named iis under iisSettings, like so:
"iis": {
"applicationUrl": "http://my.aspnetcoreapp.com"
}
Under the profiles section, add a new profile having commandName set to IIS. I am calling mine Local IIS. This will add a new option to the Run drop down named Local IIS.
"Local IIS": {
"commandName": "IIS",
"launchBrowser": true,
"launchUrl": "http://my.aspnetcoreapp.com",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
Create a website in IIS. Set host name to my.aspnetcoreapp.com. Also create/use an app pool for this website that has .NET CLR version set to "No Managed Code".
Set physical path of that website to the location of your asp.net core project, not the solution, unless of course if you have the project in the same folder as the solution.
Add a loop back entry in the hosts file (for Windows C:\Windows\System32\drivers\etc\hosts)
127.0.0.1 my.aspnetcoreapp.com
Go back to Visual Studio, and run the application. Make sure you have "Local IIS" profile selected from Run drop-down. This will launch the application in the browser, at the URL, after a brief loading message that says "Provisioning IIS...".
Done! From now on, you can launch your application at that URL, and can also debug by attaching to process w3wp.
You could also host your app under "Default Web Site", by specifying the ULR like localhost/MyAspNetCoreApp instead of my.aspnetcoreapp.com. If you do that, a new app pool will be created with the name MyAspNetCoreApp AppPool.
My medium article about this.
Simple answer: when you do publish, you call a script that launches the publish-iis tool (see script section in project.json).
In your project you have a web.config file with something like this:
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%"
stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/
As you see, there are placeholders "%LAUNCHER_PATH%" and %LAUNCHER_ARGS% parameters. Keep these in mind.
Now open your project.json file and you will see a "scripts" section looking something like this:
"scripts":
{
"postpublish":"dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%"
}
It tells dotnet to run the publish-iis tool after the application is published. How it works:
publish-iis tool goes to the folder where the application was published (not your project folder) and checks if it contains a web.config file. If it doesn’t, it will create one. If it does, it will check what kind of application you have (i.e. whether it is targeting full CLR or Core CLR and – for Core CLR – whether it is a portable or standalone application) and will set the values of the processPath and arguments attributes removing %LAUNCHER_PATH% and %LAUNCHER_ARGS% placeholders on the way.
Why need this?
This process will help while continuous development & debugging of the code, Here We do not need to deploy the application again & again and don't require to press F5 to run the application, Just change the code & build you can see the application working with the recent changes.
Description:
Prerequisite: Install the ASP.NET Core Runtime(Hosting Bundle) from the official Microsoft website.
Search for the .Net core Hosting bundle and choose Microsoft's official site.
Download the suitable version from the list
Create the .Net core Web applications and made the following changes.
Go to the folder > Properties > launchsettings.json > open the file and edit as below.
Add the below section under the existing "profiles" JSON.
"IIS": {
"commandName": "IIS",
"launchBrowser": true,
"launchUrl": "http://localhost:5000", //i.e. whatever the port set for iis
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
Now, change the "iisSettings" as below.
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iis": {
"applicationUrl": "http://localhost:5000",
"sslPort": 0
}
Go to the Solution explorer > Select the Project > "Alt + Enter" or "Right Click and Select Properties"
- Go to the Debug > General > Open debug launch profiles UI.
- Select the IIS profile from the list
- Make sure the AppURL, URL is correctly set as the IIS profile launch URL (i.e. http://localhost:5000)
Open the IIS > Sites > Add WebSite
- Site Name: Any Name of site
- Application Pool > Create or select a App pool with .NET CLR Version = No Managed Code
- Physical Path > Project folder path location
- Port: any port number i.e 5000
Build the application and run the URL - http://localhost:5000
Now, to Attach & debug the application, Go to the Visual Studio and Press Ctrl+Alt+P
Find the Process > W3wp.exe i.e In the search section enter 'W3'
Mark the checkbox checked > Show processes for all users
And Press the attach button.
Set the debug point.
Note: Visit the official Microsoft site for more detail. (Debug ASP.NET Core apps section)
https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-enable-debugging-for-aspnet-applications?view=vs-2022
Just run Ctrl + F5 and you can make code changes while running the site without restarting it.
How can I setup a .Net Core 1.0 project to use Local IIS instead of IIS Express when debugging?
I have tried modifying launchSettings.json file in various ways. For example, replacing all occurrences of IIS Express with Local IIS and updating the applicationUrl and launchUrl to use my custom localhost http://sample.local (I have updated the host file and configured IIS manager already) but not happy.
Default settings of Properties/launchSettings.json file:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:38601/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"SampleApp": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
You currently cannot directly use IIS to host an ASP.NET Core application while developing, as the development folder does not provide all of the necessary files IIS needs to host. This makes running an ASP.NET Core in a development environment a bit of a pain.
As pointed out in this article by Rick Strahl, there aren't many reasons to try and do this. IIS does very little when running ASP.NET Core apps - in fact your application no longer runs directly in the IIS process, instead it runs in a completely separate console application hosting the Kestrel web server. Therefore you really are running in essentially the same environment when you self host your console application.
If you do need to publish your app, you can do so to a local folder, using either the dotnet command line, or using the Visual Studio tools.
For example, if you want to publish to the C:\output folder, you can use the following command:
dotnet publish
--framework netcoreapp1.0
--output "c:\temp\AlbumViewerWeb"
--configuration Release
You can then point your IIS Site at the output folder. Ensure that you set the application pool CLR version to No Managed Code and that the AspNetCoreModule is available.
For more details, see https://docs.asp.net/en/latest/publishing/iis.html
Ok, so I've created a new ASP.Net 5/MVC 6 project in Visual Studio 2015 Preview. In keeping with our current method of doing things, for styling I want to use .less files. Creating the files is straightforward, but Web Essentials no longer compiles them.
So my question is this: what precisely do I need to do to get my .css files generated when I save the .less files?
Based on my adventures getting Typescript to work nicely, I will have to use Grunt to accomplish this task, but I am brand-new to Grunt and so I'm not sure how one would do it?
Please help!
With VS 2015 Web Essential is split into multiple extensions you can download
the Web Compiler extension from here and it also has details on how to use it.
It is certainly not elegant as it used to be, but if you are using existing project and want to use a compiler for LESS then this may do the basic job.
So here's how to do it (compile on build and non-elegant compile on save):
Step 1
Open up your package.json file (it's in the root of your project) and add these lines:
"grunt-contrib-less": "^1.0.0",
"less": "^2.1.2"
Obviously you can change the version numbers (you'll get helpful intellisense), these are just the current versions.
Step 2
Right-click on the NPM folder (under Dependencies) and click Restore Packages. This will install less and grunt-contrib-less.
Step 3
Once those packages are restored, go to your gruntfile.js file (again, in the root of the project). Here, you'll need to add the following section to grunt.initConfig
less: {
development: {
options: {
paths: ["importfolder"]
},
files: {
"wwwroot/destinationfolder/destinationfilename.css": "sourcefolder/sourcefile.less"
}
}
}
You'll also need to add this line near the end of gruntfile.js:
grunt.loadNpmTasks("grunt-contrib-less");
Step 4
Then just go to View->Other Windows->Task Runner Explorer in the menu hit the refresh icon/button, then right-click on less under Tasks and go to Bindings and tick After Build.
Hooray, now less files will compile and we (I) learned about grunt, which seems really powerful.
Step 5: Compiling on save
I still haven't got this working to my satisfaction, but here's what I've got so far:
As above, add another NPM package grunt-contrib-watch (add to package.json, then restore packages).
Then add a watch section in gruntfile.js, like this (obviously this can work for other types of files as well):
watch: {
less: {
files: ["sourcefolder/*.less"],
tasks: ["less"],
options: {
livereload: true
}
}
}
So you'll now have something like this in your gruntfile.js:
/// <binding AfterBuild='typescript' />
// This file in the main entry point for defining grunt tasks and using grunt plugins.
// Click here to learn more. http://go.microsoft.com/fwlink/?LinkID=513275&clcid=0x409
module.exports = function (grunt) {
grunt.initConfig({
bower: {
install: {
options: {
targetDir: "wwwroot/lib",
layout: "byComponent",
cleanTargetDir: false
}
}
},
watch: {
less: {
files: ["less/*.less"],
tasks: ["less"],
options: {
livereload: true
}
}
},
less: {
development: {
options: {
paths: ["less"]
},
files: {
"wwwroot/css/style.css": "less/style.less"
}
}
}
});
// This command registers the default task which will install bower packages into wwwroot/lib
grunt.registerTask("default", ["bower:install"]);
// The following line loads the grunt plugins.
// This line needs to be at the end of this this file.
grunt.loadNpmTasks("grunt-bower-task");
grunt.loadNpmTasks("grunt-contrib-less");
grunt.loadNpmTasks("grunt-contrib-watch");
};
One can then simply set this task to run on Project Open (right-click on watch under Tasks in the Task Runner Explorer (it's under View->Other Windows in the top menu) and you're done. I would expect you'd have to close and re-open the project/solution to get this to kick in, otherwise you can manually run the task.
(Note: there is now a new question asked here directly concerning sass. I tried to alter the question and tags in this question to include sass, but someone didn't allow it.)
I would like to add the answer to the same question for sass (.scss). The answer is so related I think these may best be combined as two answers in this same post (if you disagree, please let me know; else, we might add "or sass" in the post title?). As such, see Maverick's answer for some fuller details, here's the nutshell for sass:
(Pre-step for Empty Projects)
If you started with an empty project, first add Grunt and Bower:
Right click solution -> Add -> 'Grunt and Bower to Project' (then wait for a minute for it to all install)
package.json:
"devDependencies": {
"grunt": "^0.4.5",
"grunt-bower-task": "^0.4.0",
"grunt-contrib-watch": "^0.6.1",
"grunt-contrib-sass": "^0.9.2"
}
(FYI: grunt-contrib-sass link)
Then:
Dependencies -> right-click NPM -> Restore Packages.
gruntfile.js
1) Add or make sure these three lines are registered near the bottom (as NPM tasks):
grunt.loadNpmTasks("grunt-bower-task");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-contrib-sass");
2) Again in gruntfile.js, add init configurations, something like the following.
{ Caveat: I am no expert on such configurations. I found the sass configuration on an excellent blog post some time ago that I can't locate at this time in order to give credit. The key was I wanted to find all files in the project within a certain folder (plus descendants). The following does that (notice "someSourceFolder/**/*.scss", and see important related note here). }
// ... after bower in grunt.initConfig ...
"default": {
"files": [
{
"expand": true,
"src": [ "someSourceFolder/**/*.scss" ],
"dest": "wwwroot/coolbeans", // or "<%= src %>" for output to the same (source) folder
"ext": ".css"
}
]
},
"watch": {
"sass": {
"files": [ "someSourceFolder/**/*.scss" ],
"tasks": [ "sass" ],
"options": {
"livereload": true
}
}
}
Now follow the instructions for Task Runner Explorer as given in the other answer. Make sure to close and reopen project. It seems you have to run (double click) 'watch' (under 'Tasks') every time the project is started to get the watch watching, but then it works on subsequent saves.