Some pitfalls recently encountered when using. NET Core,. netcore

Source: Internet
Author: User

Some pitfalls recently encountered when using. NET Core,. netcore

 

 

Recently, after the upgrade of. NET Core to 2.0, it began to gradually increase, but encountered a lot of pitfalls, so it was specially recorded.

The first pitfall condition Compiler

When writing some methods, we usually add some output logs for the Debug mode for us to check and add or modify some specific parameters for the Release mode, but today, I encountered this challenge when I wrote this article.
# If! DEBUG # the code in the middle of endif cannot be changed. How can I change the configuration Environment and the Environment is always grayed out? Is it true that the. NET Core of VS 2017 does not support conditional compilation characters?

Therefore, a new console Program was created to test the following, but it still worked as follows:

Here we can see that I am clearly not a Debug environment, but # if DEBUG is still normal color ,! DEBUG is still gray. I was surprised when I ran F5 directly.

The result is normal. I suspect that something went wrong after vs was updated. So I created a project in the old format of. net framework and found that the old one is good.

Part of files under. NET Core MVC cannot be downloaded.

  Use. NET Core MVC created a site, which was originally used quite well, but later it was equipped with an app so it directly put the apk file under the wwwrootdirectory of the website, changed its name to app.apk, and then accessed: http: // 127.0.0.1/app.apk returns a 404 not find

 

Because there are still a lot of iis, I immediately thought of adding mime on a daily basis, so I went to the iis Site to add it, and found that it exists.

In an instant, I tried to find out whether the file was disabled but not found in the request filtering. Then I changed the file to app.apk.zip and found that the zip file can be downloaded.

 

 

 

 

------------

Updated at noon on the 21st,

Thanks to @ snail forward for your guidance, I remembered that because iis is only a proxy, I had a solution, that is, configuring it in the appsettings. json configuration.

As follows:

Add code to set Startup. cs

 public void ConfigureServices(IServiceCollection services)        {            services.Configure<Dictionary<string,string>>(Configuration.GetSection("Mime"));            services.AddMvc();            services.AddDbContext<ApplicationDataContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServerConnection")));        }
Public void Configure (IApplicationBuilder app, IHostingEnvironment env, IOptions <Dictionary <string, string> option) {if (env. isDevelopment () {app. usemediaexceptionpage (); app. useBrowserLink ();} else {app. useExceptionHandler ("/Home/Error");} // app. useStaticFiles () // use var provider = new FileExtensionContentTypeProvider (); foreach (string key in option. value. keys) {provider. map Pings. add (key, option. value [key]);} app. useStaticFiles (new StaticFileOptions () {ContentTypeProvider = provider}); app. useMvc (routes => {routes. mapRoute (name: "default", template: "{controller = Home}/{action = Index}/{id ?} ");});}

 

Because the default constructor mime of FileExtensionContentTypeProvider is almost dead. . NET core websites do not adopt iis settings.
FileExtensionContentTypeProvider Code address you can go to see https://github.com/aspnet/StaticFiles/blob/dev/src/Microsoft.AspNetCore.StaticFiles/FileExtensionContentTypeProvider.cs

 

Third trap. NET Core 2.0 MVC attempt File

Starting from 2.0, it seems that the file is directly packaged into a dll file. Instead of being published as a shtml file like the traditional mvc, It is compiled into a dll file. The naming rule is the project name. PrecompiledViews. dll.

 

 The fourth pitfall. NET Core references DLL

In the past, developers always made some commonly used functions into a single class library and compiled them into a dll for the project to use. However, it seems that this does not work in the. NET Core project.

At first, I wrote a public class library and added a class library in the solution to reference the project of the public class library. In this case, there was no exception, however, when I start another vs to create a new solution to add a project, after referencing the dll of the public class library, it is normal to write code in vs, and the code prompts are also available.

However, when you press F5 for debugging, the system prompts that the type or namespace cannot be found.

 

The solution is to package the public class library to generate the NuGet package.

 

Then add references by managing NuGet Packages, but in many cases I don't want to put some class libraries on nuget.org. I can place the generated nuget Packages in the Microsoft Visual Studio Offline Packages Offline package.

Go to the Microsoft Visual Studio Offline Packages directory.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.