How to Publish ASP.NET Core WEB API Project on Local IIS in Visual Studio 2017 Community RC? - iis

I want to publish ASP.NET Core WebAPI Project with swagger on IIS. i want to run the Project on local machine with IP Address.
Configuration of My PC is:
Visual Studio 2017 Community RC
Windows 10
IIS
Please help me.

Have you tried "this one (learn.microsoft.com)"?
It helped for me. Don't forget to configure authentication and access to your site physical path for application pool identity you are going to use.
UPD (reply to comment):
In case you're using Swashbuckle.AspNetCore package you can try somethig like this in your ConfigureServices(IServiceCollection services):
// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen(c =>
{
var info = new Info
{
Title = "My web API",
Version = "v1.0",
Contact = new Contact()
{
Email = "foo#gmail.com",
Name = "Denis",
Url = "https://stackoverflow.com"
},
Description = "My API"
};
c.SwaggerDoc("v1.0", info);
var basePath = AppContext.BaseDirectory; // this is not equal to wwwroot folder due to WebHostBuilder settings
var xmlPath = Path.Combine(basePath, "MyApi.xml");
c.IncludeXmlComments(xmlPath);
});
And in Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory):
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1.0/swagger.json", "My web API v1.0");
c.EnableDeepLinking();
});

Related

ASP.NET Core 6 MVC published to local IIS routing not working HTTP 500 error

I have published an ASP.NET Core 6 MVC web application to my local IIS. The site loads without error, however the routing doesn't work and I am left with a HTTP 500 error when navigating from the home page.
The routing works when I build and load directly from Visual Studio. I am not sure what elements you would need to help me with this, but here is my program.cs.
Help please :). Richard
using Microsoft.EntityFrameworkCore;
using RabsoInc.Web.DAL;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
ConfigurationManager configuration = builder.Configuration;
builder.Services.AddMvc();
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("default")));
builder.Services.AddTransient<IDomain, DomainRepo>();
builder.Services.AddTransient<IItem, ItemRepo>();
var app = builder.Build();
app.UseRouting();
app.UseStaticFiles();
app.UseDefaultFiles();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
app.Run();

openapi.json not found when hosting NetCore 3.1 Web Api on IIS

I generated my server stub for ASP.NET Core 3.1 using the openapi generator with the following command:
npx #openapitools/openapi-generator-cli generate -i myapi.json -g aspnetcore -o C:\myapi --additional-properties aspnetCoreVersion=3.1
After opening that project in Visual Studio 2019 and running it I get the swagger page where I can try out the different endpoints. So far OK.
However: after publishing this on our IIS Server and visiting the proper URL that swagger page is loading with the error:
Not found: /swagger/1.0.0/openapi.json
Swagger error on IIS
The endpoint itself (GET request) can be reached.
The Swagger Endpoint code in startup.cs is the default code as it was generated by open-api generator:
app.UseSwagger(c =>
{
c.RouteTemplate = "swagger/{documentName}/openapi.json";
})
.UseSwaggerUI(c =>
{
//TODO: Either use the SwaggerGen generated Swagger contract (generated from C# classes)
c.SwaggerEndpoint("/swagger/1.0.0/openapi.json", "Swagger WP RUH Delivery");
//TODO: Or alternatively use the original Swagger contract that's included in the static files
// c.SwaggerEndpoint("/openapi-original.json", "Swagger WP RUH Delivery Original");
});
Why is this Swagger page working on my machine, but on IIS things go wrong?
I found some hints on changing the c.SwaggerEndpoint in startup.cs, but nothing helped.
Try to remove /swagger/ from the URL path:
c.SwaggerEndpoint("1.0.0/openapi.json", "Swagger WP RUH Delivery")

mapbox files pbf blocked IIS server

PBF (street map mapbox vector files) files are not allowed to be served /downloaded from IIS (2008 R8) and I need them to be.
The background
PBFs are served OK when using the react development server
//Startup.cs
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
These files will appear on the map correctly.
However when deploying the .NET Core app to IIS with
ASPNETCORE_ENVIRONMENT = production
set. These files are essentially blocked.
I have added the MIME type
I believe this is an IIS thing as like I say, on the react server in development they load fine.
Any clues as of why they still won't download?
Thanks
Basically IIS virtual directories aren’t supported in .net core. Due to the way .net core projects are served in IIS using a reverse proxy. So in the startup.cs file, do something like this:
// Configure the virtual directory
app.UseStaticFiles(new StaticFileOptions {
FileProvider = new PhysicalFileProvider(#"\\Server\Directory\.."),
RequestPath = "/NameOfDirectory",
ContentTypeProvider = provider,
OnPrepareResponse = (context) => {
if (!context.Context.User.Identity.IsAuthenticated) {
context.Context.Response.Redirect("LoginPage");
}
}
});

Get HttpStatusCode 504 ( DNS Name Not Found ) in .Net Core 2.1 and Upper Version

I was create simple project with .netCore 2.0 and send HttpRequest with HttpClient, that is working well.
But, when I'm migrating from .netCore 2.0 to upper version ( e.g: .NetCore 2.1 or .netCore 3.0 ) this Code is not working.
My Code is:
public async Task<bool> IsValid()
{
string url = "http://api.domain.com/...";
var values = new Dictionary<string, string>()
{
{ "param1", "value1" },
{ "param2", "value2" }
};
HttpClient client = new HttpClient();
var content = new FormUrlEncodedContent(values);
var post = await client.PostAsync(url, content);
if (post.StatusCode == System.Net.HttpStatusCode.OK)
{
return true;
}
return false;
}
I expect the output of httpResponse to bo HttpStatusCode.OK. but the actual output is HttpStatusCode.GatewayTimeout.
I found that:
If I run API server ( http://api.domain.com/ ) in IIS of windows server 2012, all requests is working well.
But When I Use IIS of Windows 8, only HttpRequest with ASP.NET Core sdk 2.0 is working and others not working.
Can anyone help me?
This problem has finally been resolved.
This problem was related to the network and proxy settings in my network.
I realize when api server be in internet network, that is working well. but when api server to be used in local network, Because of using the proxy in my network, all
requests was encountered with error 504 (unless the request was sending with .netCore sdk 2.0 ).
It should be noted I had added this line 192.168.11.125 api.domain.com to host file in Directory c:\windows\system32\drivers\etc\hosts.
and also, I had added Follow Exceptions:
192.168.11.125 api.domain.com
from path:
Control Panel > Network and Internet > Internet Option > Connections Tab > Lan Settings > Advanced > Exceptions panel
But that's not working.
When I picked up the check of Use a proxy server for your lan ... in Local Area Network (LAN) Settings form, all requests working well.
Of course, this question remains that, why The same request is working with .netcore sdk 2.0 ?
what is difference between sending request with .netCore sdk 2.0 and (.netCore sdk 2.1 or .netCore sdk 2.2 or .netCore sdk 3.0)???!!!
Good luck.

Application Insights to azure webjob .Net Core 2.0

How to add application insights telemetry (Application Insights) to azure webjob ?
With recently released WebJob SDK 3.0, you can add ApplicationInsights in the ConfigureLogging method
public static async Task Main(string[] args)
{
var builder = new HostBuilder()
.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices().AddAzureStorage();
})
.ConfigureAppConfiguration(b =>
{
// Adding command line as a configuration source
b.AddCommandLine(args);
})
.ConfigureLogging((context, b) =>
{
b.SetMinimumLevel(LogLevel.Debug);
b.AddConsole();
// If this key exists in any config, use it to enable App Insights
string appInsightsKey = context.Configuration["ApplicationInsights:InstrumentationKey"];
if (!string.IsNullOrEmpty(appInsightsKey))
{
b.AddApplicationInsights(o => o.InstrumentationKey = appInsightsKey);
}
});
var host = builder.Build();
using (host)
{
await host.RunAsync();
}
}
You can add AI to Web Jobs during the development time as a Nuget Package.
AI .NET Core Nuget is here. Package name is a bit misleading (Microsoft.ApplicationInsights.AspNetCore) but it's supposed to work with all .Net core apps.
AI .NET Core GitHub page is here (with some customization options explained in Wiki).
Getting started guide is on GitHub and learn.microsoft.com as well. It's a bit lengthy guide, so I hope links are OK (although not in full accordance with SO guidelines) and I won't need to post it as part of the reply.

Resources