milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home

Start empty Asp.Net core project

Details
Written by: Stanko Milosev
Category: Core
Published: 27 June 2020
Last Updated: 05 April 2022
Hits: 2770
  • core
One my example how to start empty Asp.Net core project in Visual Studio 2019, and add one controller to it.

Start empty Asp.Net core project:

Choose ASP.NET Core Web Application:

Choose "Empty":

Since I am not using reSharper, I had to create "Controllers" folder manually. Open some file from solution and right click on tab and open containing folder:

Create new folder and name it "Controllers":

Your folder structure now should look like:

In "Controllers" folder in create new file, and Name it like EmptyController.cs, write something like

using Microsoft.AspNetCore.Mvc;

namespace AspDotNetCorePostExample.Controllers
{
  [Route("api/[controller]")]
  [ApiController]
  public class EmptyController : ControllerBase
  {
    [HttpGet]
    public string Get()
    {
      return "Hello world";
    }
  }
}
In AspDotNetCorePostExample\AspDotNetCorePostExample\Startup.cs in method "Configure" delete:
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
	  {
	await context.Response.WriteAsync("Hello World!");
  });
});
Instead write:
app.UseEndpoints(endpoints =>
{
	endpoints.MapControllers();
});
In ConfigureServices add services.AddControllers();

Start solution and go to like "https://localhost:44336/api/Empty" (just add /api/Empty)

If you like you can manually add wwwroot folder, and use him like I already explained here. Here is new Startup.cs, and to disable HTPPS go to project properties -> Debug -> uncheck "Enable SSL":

wwwroot

Details
Written by: Stanko Milosev
Category: Core
Published: 01 February 2020
Last Updated: 09 November 2021
Hits: 3176
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.

Enable Cross-Origin Requests (CORS) in ASP.NET Core

Details
Written by: Stanko Milosev
Category: Core
Published: 24 January 2020
Last Updated: 24 January 2020
Hits: 2679
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:

readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
Then:
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");
	});
  });
}
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();
    }
  }
}

Debug Asp.Net core on IIS by attaching to process

Details
Written by: Stanko Milosev
Category: Core
Published: 01 January 2020
Last Updated: 06 April 2022
Hits: 2515
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":

  1. Debug Asp.Net core on IIS in VS2017
  2. Publish Asp.Net core to IIS
  3. Code snippet
  4. Example of marker with custom icon (image)

Subcategories

C#

Azure

ASP.NET

JavaScript

Software Development Philosophy

MS SQL

IBM WebSphere MQ

MySQL

Joomla

Delphi

PHP

Windows

Life

Lazarus

Downloads

Android

CSS

Chrome

HTML

Linux

Eclipse

Page 52 of 164

  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56