I have created an empty web application in visual studio code. But when running it using "dotnet run" it is not opening any browser
it is only showing the below lines.
>C:\Users\viru babu\Documents\MVCcoreLinkdin>dotnet run
>Hosting environment: Production
>Content root path: C:\Users\viru babu\Documents\MVCcoreLinkdin
>Now listening on: http://localhost:5000
>Application started. Press Ctrl+C to shut down.
To open the browser automatically on http://localhost:5000 you can add a configuration to your launch.json. Add a configuration like this:
{
"name": "Chrome",
"type": "chrome",
"runtimeExecutable": "./path/to/chrome.exe",
"request": "launch",
"url": "http://localhost:5000",
"webRoot": "${workspaceRoot}", // maybe you need to adjust this entry to "C:\Users\viru babu\Documents\MVCcoreLinkdin"
"preLaunchTask": "shell: dotnet run" // add prelaunch task start (defined in tasks.json)
}
to also launch your application automatically you use the preLaunchTask. To do that you need to configure a custom task in the tasks.json that runs your command dotnet run.
Related
In school we have a project where we are working with vite/vuejs as frontend and using ASP.NET core web API as the rest/backend.
I work on the backend/rest-api in visual studio and the frontend bit in VS Code. Right now to run the website, i have to start the ASP.net API in visual studio and then run the "npm run dev" command in vs code to start the website itself.
Those two are running on seperate "servers" and diffrent ports.
So my real question is, how can i make those two run on the same "server" and port.
So when i run the project, i would like both the backend/rest-api and the frontend to run as "one".
I think the service is IIS.
It is possible to achieve that (with similar result to VueCliMiddleware when using VueCLI), at least for .net core 5 and 6.
What you need to do is follow this blog post where you need to add and configure SpaServices in your Startup.cs file: https://blogs.taiga.nl/martijn/2021/02/24/integrating-vite-with-asp-net-core-a-winning-combination/
I got this working as well for my project but I had to make some changes in npm scripts because other routes where hijacked by SPA portion:
//Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseResponseCaching();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
if (env.IsDevelopment())
{
app.UseSpa(spa =>
{
// use 'build-dev' npm script
spa.UseReactDevelopmentServer(npmScript: "build-dev");
});
}
}
// package.json
// new script "build-dev" for building vue in development mode
{
"name": "vueapp",
"version": "0.0.0",
"scripts": {
"dev": "echo Starting the development server && vite",
"build": "vite build",
"build-dev": "echo Building for development && vite build --mode development",
"preview": "vite preview",
},
"dependencies": {
"vue": "^3.2.25",
"vue-router": "^4.0.12"
},
"devDependencies": {
"#vitejs/plugin-vue": "^2.0.0",
"eslint": "^8.5.0",
"eslint-plugin-vue": "^8.2.0",
"sass": "^1.45.0",
"vite": "^2.7.2",
"vue-eslint-parser": "^8.0.1"
}
}
You can launch it from solution but I really prefer using dotnet watch run in terminal - you have hot reloading for asp.net stuff but the downside of this setup that hot reloading doesn't work for Vue.
Anyway, give it a shot.
ASP.NET Core officially supports hosting a static files server.
app.UseStaticFiles();
The default hosting folder is wwwroot.
So if you want to host it with the same server, you can simply configure your project to publish all the front-end vue stuff to the wwwroot folder.
Remember, to publish the front-end built files to wwwroot, not the source file.
And after publishing front-end code, simply start your web server and it works just fine.
You can create a binding to make Visual Studio auto run some npm build commands while building the ASP.NET Core project. So you can build once and start debugging.
Maybe try this template.
https://marketplace.visualstudio.com/items?itemName=MakotoAtsu.AspNetCoreViteStarter
before I always use vue + VueCliMiddleware + asp.net core so I try to use vite and
I found this while exploring and trying to use vite in asp.net core in visual studio 2022.
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 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.
I'm facing strange issue - I deployed my Meteor app with mup ( followed this instruction: https://www.vultr.com/docs/deploy-a-meteor-application-on-ubuntu ). Everything went successfully (see screenshot).
However the content doesn't appear when I try to access it via browser (I type IP-address).
As a server I use apache2 on Ubuntu 15.04, and mup.json as following:
{
// Server authentication info
"servers": [
{
"host": "IP_ADDRESS",
"username": "developer"
//"password": "password"
// or pem file (ssh based authentication)
//"pem": "~/.ssh/id_rsa"
}
],
// Install MongoDB in the server, does not destroy local MongoDB on future setup
"setupMongo": false,
// WARNING: Node.js is required! Only skip if you already have Node.js installed on server.
"setupNode": true,
// WARNING: If nodeVersion omitted will setup 0.10.36 by default. Do not use v, only version number.
"nodeVersion": "0.10.36",
// Install PhantomJS in the server
"setupPhantom": true,
// Show a progress bar during the upload of the bundle to the server.
// Might cause an error in some rare cases if set to true, for instance in Shippable CI
"enableUploadProgressBar": true,
// Application name (No spaces)
"appName": "meteor",
// Location of app (local directory)
"app": "~/app",
// Configure environment
"env": {
"PORT": 80,
"UPSTART_UID": "meteoruser",
"ROOT_URL": "http://IP_ADDRESS",//and I also tried http://localhost
//"MONGO_URL": "",
"METEOR_ENV": "production"
},
// Meteor Up checks if the app comes online just after the deployment
// before mup checks that, it will wait for no. of seconds configured below
"deployCheckWaitTime": 15
}
I would appreciate any help! Thanks in advance!
UPD: I made a better research and found a great tutorial - https://www.phusionpassenger.com/library/walkthroughs/deploy/meteor/ownserver/apache/oss/vivid/deploy_app.html . So, if anyone else curious about how to deploy Meteor app, please have a look :)
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