In order to serve static files in Asp.Net Core you will need following line in Startup.cs method configure:
app.UseStaticFiles();
When deploying in order to show default document, add following line:
app.UseDefaultFiles();
My whole Startup.cs looks like this:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace AllPics2gMaps
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
app.UseDefaultFiles();
app.UseStaticFiles();
}
}
}
UPDATE; Please note thatStartup.cs has been changed, refer to this article.
Title of this article I have took from here.
If you are receiving a message like:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://test:2021/api/values. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)
This means that you have to enable CORS (since 2014)
In my case I have created new ASP.NET Core WebApi on the link http://test:2021/api/values, and I was accessing that link from http://gallery.milosev.com:9090.
Open Startup.cs:
First add line:
Here notice: http://gallery.milosev.com:9090, and at the end:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors(MyAllowSpecificOrigins);
app.UseHttpsRedirection();
app.UseMvc();
}
Whole Startup.cs:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace EnableCORS
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddCors(options =>
{
options.AddPolicy(MyAllowSpecificOrigins,
builder =>
{
builder.WithOrigins("http://gallery.milosev.com:9090");
});
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors(MyAllowSpecificOrigins);
app.UseHttpsRedirection();
app.UseMvc();
}
}
}
Here I explained how to publish code to IIS, and here I explained one way to debug your application, but I don't like it, since it is taking too much time to start. Here I have found another way to debug asp.net core application, from my point of view much better. Here I already explained hot to attach to process, the only difference ist that you have to attach to "dotnet.exe":
To write this article I was using Development-time IIS support in Visual Studio for ASP.NET Core.
First in Visual Studio installer enable development-time IIS support in Visual Studio:
Create new Asp.Net Core MVC application.
Add application to IIS, as physical path in my case I added "C:\Users\pera\Documents\Visual Studio 2017\Projects\WebApplication1\WebApplication1":
In Visual Studio right click on project go to properties:
Go to Debug, click on new:
In profile name write IIS:
Launch: IIS, Select the check box for Launch browser: http://test:2021/, the Environment variables section, select the Add button. Provide an environment variable with a Name of ASPNETCORE_ENVIRONMENT and a Value of Development:
Restart Visual Studio as administrator, if you haven't already.
Set the Start Debugging button to the IIS profile and select the button to start the app: