How do you add a mime type when using ASP.NET vNext? - iis-express

There's LOADS of information on how to add MIME types into a normal project.
These include configuring IIS, or modifying the web.config.
Both of these options are unavailable to me in vNext with IIS Express.
I had a look at the schema to the project.json file and couldn't find anything in there that would help either.
Can this be done yet? - I want to add a mime type for the .woff2 extension.

If you hosting it on IIS 7 or later then following step will do what you need. This answer I have used Visual Studio 2015 CTP5.
Publish your web application ( ASP.net vnext)
You can publish it to location like C:\MyPublish
Once it get successfully published you will find following location C:\MyPublish\wwwroot. Here You will find web.config.
Now host your site to in IIS ( Make sure that you have used C:\MyPublish\wwwroot as your path)
Now edit web.config over here just like you did for old version to add mime type. ( Following is my edit)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="kpm-package-path" value="..\approot\packages" />
<add key="bootstrapper-version" value="1.0.0-beta2" />
<add key="kre-package-path" value="..\approot\packages" />
<add key="kre-version" value="1.0.0-beta2" />
<add key="kre-clr" value="CLR" />
<add key="kre-app-base" value="..\approot\src\WebApplication5" />
</appSettings>
<system.webServer>
<staticContent>
<remove fileExtension=".woff" />
<mimeMap fileExtension=".woff" mimeType="application/font-woff" />
</staticContent>
</system.webServer>
</configuration>
Note: As per my thinking In old version it is fix that it is always windows environment so we have direct web.config file in project and we edit that but now we have to follow different process to register as in future we can host completly on linux env as well.
Update : There is another way to do that as well. If you are using Microsoft.AspNet.StaticFiles package then you will have extension.
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
}
This will indirectly use https://github.com/aspnet/StaticFiles/blob/dev/src/Microsoft.AspNet.StaticFiles/FileExtensionContentTypeProvider.cs. Here you can see all mapping.
Update 2: (Add New Mime Type)
public void Configure(IApplicationBuilder app)
{
StaticFileOptions option = new StaticFileOptions();
FileExtensionContentTypeProvider contentTypeProvider = (FileExtensionContentTypeProvider)option.ContentTypeProvider;
contentTypeProvider.Mappings.Add("<<yourextention>>","<<mimetype>>");
app.UseStaticFiles(option);
}

Until this is released, you can also edit applicationhost.config which I found in D:\Documents\IISExpress\config (yours might be on your C drive [Documents]).
I added:
<mimeMap fileExtension=".woff2" mimeType="font/x-woff2" />
Inside <staticContent>.

Related

How to load .gltf in a Blazor application

I am trying to load .gltf model using three.js into my Blazor application.
However the server does not serve this type of files.
I am aware that MIME type must be added, but for some reasons, that cannot be done with Blazor web app as the 'app' variable in Startup.cs is an instance of IComponentsApplicationBuilder. Can anybody help me with this issue.
IIS and IIS Express will not serve files with unknown extensions. In your error console, you see 404 (Not Found) which means either the file is missing, or the MIME type for the file is not registered.
I would recommend you try adding a web.config file to the root of your application, with the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<remove fileExtension=".gltf" />
<mimeMap fileExtension=".gltf" mimeType="model/gltf+json" />
<remove fileExtension=".glb" />
<mimeMap fileExtension=".glb" mimeType="model/gltf-binary" />
<remove fileExtension=".bin" />
<mimeMap fileExtension=".bin" mimeType="application/octet-stream" />
</staticContent>
</system.webServer>
</configuration>
The <remove ... /> statements are there to avoid any possible conflicts with any MIME type registrations that happen in parent folders or at the root-level or system-level. It's always safe to remove, but it's a configuration error to add one that already exists.
Here's a reference to where the glTF Mime Type was defined.
Some versions of IIS Express will disregard MIME types from web.config. If this happens, the above file may not work. In that case you may have to edit the IIS Express configuration file directly, to add the information shown above. Check this SO answer to see how to locate that config file.
There's no need to mess around with web.config.
You just need to inject the StaticFileOptions from Program.cs.
using Microsoft.AspNetCore.StaticFiles;
...
builder.Services.Configure<StaticFileOptions>(options =>
{
options.ContentTypeProvider = new FileExtensionContentTypeProvider
{
Mappings =
{
[".gltf"] = "model/gltf+json",
[".glb"] = "model/gltf-binary",
[".bin"] = "application/octet-stream"
}
};
});
Full docs with an alternative option:
https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/static-files?view=aspnetcore-6.0#blazor-server-file-mappings-and-static-file-options

.NET Core 3.0: ASPNETCORE_ENVIRONMENT has value Development on publish

I tried to migrate my ASP.NET Core 2.2 project to the newly released ASP.NET Core 3.0 over the weekend.
Everything looked good in the local environment, but after publishing and deploying to IIS, I faced a few issues as it was using the development environment configurations.
I am using Visual Studio 2019 Community Edition version 16.3.0
Upon inspection, I found that the web.config file had the ASPNETCORE_ENVIRONMENT value set to Development, which was causing the issue. It was generated with the web publish even in Release configuration.
I thought it was supposed to be Production? Or did I miss some configuration? I‘ve never faced this issue with any earlier versions of .NET Core.
Now the issue is that if I publish the whole folder again, the issue is likely to come back.
Any solutions or suggestion regarding the root cause of the issue? My Web.config looks like the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<httpRuntime executionTimeout="180" />
</system.web>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\MyApp.dll" forwardWindowsAuthToken="false" stdoutLogEnabled="false" startupTimeLimit="3600" requestTimeout="23:00:00" hostingModel="InProcess" stdoutLogFile=".\logs\stdout">
<environmentVariables>
<environmentVariable name="ASPNETCORE_HTTPS_PORT" value="44329" />
<environmentVariable name="COMPLUS_ForceENC" value="1" />
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
Check your csproj file and remove below code if it exists.
<PropertyGroup>
<EnvironmentName>Development</EnvironmentName>
</PropertyGroup>
Also check the Properties/PublishProfiles/{profilename.pubxml}.This will set the Environment name in web.config when the project is published.
Refer to How to set aspnetcore_environment in publish file?

Autogenerated web.config not accepted by IIS 10

I want to publish a entity framework core (.Net) to a IIS.
For this I publish the application in Visual Studio Community 2017. When I publish the application the web.config file will be auto generated as the following
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\basket.api.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
</system.webServer>
</location>
</configuration>
<!--ProjectGuid: ba174064-1719-4e47-a0c3-5e8cff312047-->
I go to my IIS 10
I post it into the default website. Into to the location "C:\inetpub\wwwroot".
I start the web site an access via the webbrowser localhost.
I get allways the following error message:
I check the web page of Microsoft of the error code 0x8007000d,it is basicly saying that the format of a XML Element is not correct. Which is impossible I check all the formating and also I let autogenerate by the Visual Studio when i publish the site. I tried everything and don't know what to do. I am very newbie in IIS stuff.

Configuration of ASP.Net Core

I have a web project that the front-end is Angular 5 and the back-end is ASP.NET Core. the front-end and back-end are in separated projects.
I have a host with Windows as OS and Plesk as control panel, the Document root folder is home and placed in Home directory
I publish my api to home/api directory but I get some errors about not started application(502), when I publish my api to home directory, I can not access my front-end files and I get page can’t be found error
here is my web.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\Api.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
</system.webServer>
Is there any way to publish the API on any folder? like home/api

Configuring Lightspeed for use in MVC3 Web project

I have a question regarding using LightSpeed in a MVC3 Web application. I am creating a REST Api project where I want to use LightSpeed, but the official configuration guidelines are quite vague. The official documentation says that the following lines need to be added to the file web.config:
<configSections>
<section name="lightSpeedContexts"
type="Mindscape.LightSpeed.Configuration.LightSpeedConfigurationSection, Mindscape.LightSpeed" />
</configSections>
<lightSpeedContexts>
<add name="Test" />
</lightSpeedContexts>
<lightSpeedContexts>
<add name="Test" dataProvider="SQLite3" />
</lightSpeedContexts>
I tried adding following lines to the web.config in the root:
<configSections>
<section name="lightSpeedContexts"
type="Mindscape.LightSpeed.Configuration.LightSpeedConfigurationSection, Mindscape.LightSpeed" />
</configSections>
<lightSpeedContexts>
<add name="Default" connectionStringName="Prod" dataProvider="MySQL5" />
</lightSpeedContexts>
<connectionStrings>
<add name="Prod" connectionString="server=localhost;User Id=production;password=xxx;Persist Security Info=True;database=CBS"/>
</connectionStrings>
This throws an exception when I start the Web application which tells me that configSections may not be specified more than once in the application. The root web.config file did not have any of these specified per default.
I am not sure where to put this configuration.
OK, I found out how to solve the problem.
I moved out the configSections that were in the two sub web.config files (in each Views folder) and put the contents in the main web.config file, then I added the lightSpeedContexts and connectionStrings in the main web.config file.

Resources