Repository navigation
Update migration guide /8 - #28184
Merged
Merged
Conversation
Rick-Anderson
commented
Jan 25, 2023
Comment on lines
+22
to
+36
| ## URI decoding differences between ASP.NET to ASP.NET Core | ||
|
|
||
| ## Prerequisites | ||
| ASP.NET Core has the following URI decoding differences with ASP.NET Framework: | ||
|
|
||
| [.NET Core SDK 2.2 or later](https://dotnet.microsoft.com/download) | ||
| | ASCII | Encoded | ASP.NET Core | ASP.NET Framework | | ||
| | ------------- | ------------- | ------------- | ------------- | | ||
| | `\` | `%5C` | `/` | `\` | | ||
| | `/` | `%2F` | `%2F` | `/` | | ||
|
|
||
| ## Target frameworks | ||
| `%2F`: | ||
|
|
||
| ASP.NET Core projects offer developers the flexibility of targeting .NET Core, .NET Framework, or both. See [Choosing between .NET Core and .NET Framework for server apps](/dotnet/standard/choosing-core-framework-server) to determine which target framework is most appropriate. | ||
| * The entire path gets unescaped except `%2F` because converting it to `/` would change the path structure. It can’t be decoded until the path is split into segments. | ||
| * Aee the [PathBase source](https://source.dot.net/#Microsoft.AspNetCore.Http.Abstractions/HttpRequest.cs,8d85f458c32cb4a5) for more information. | ||
|
|
||
| When targeting .NET Framework, projects need to reference individual NuGet packages. | ||
|
|
||
| Targeting .NET Core allows you to eliminate numerous explicit package references, thanks to the ASP.NET Core [metapackage](xref:fundamentals/metapackage-app). Install the `Microsoft.AspNetCore.App` metapackage in your project: | ||
|
|
||
| ```xml | ||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.AspNetCore.App" /> | ||
| </ItemGroup> | ||
| ``` | ||
|
|
||
| ## Project structure differences | ||
|
|
||
| The `.csproj` file format has been simplified in ASP.NET Core. Some notable changes include: | ||
|
|
||
| - Explicit inclusion of files isn't necessary for them to be considered part of the project. This reduces the risk of XML merge conflicts when working on large teams. | ||
| - There are no GUID-based references to other projects, which improves file readability. | ||
| - The file can be edited without unloading it in Visual Studio: | ||
|
|
||
|  | ||
|
|
||
| ## Global.asax file replacement | ||
|
|
||
| ASP.NET Core introduced a new mechanism for bootstrapping an app. The entry point for ASP.NET applications is the *Global.asax* file. Tasks such as route configuration, filter and area registrations are handled in the *Global.asax* file: | ||
|
|
||
| [!code-csharp[](~/migration/proper-to-2x/samples/globalasax-sample.cs)] | ||
|
|
||
| This approach couples the application and the server to which it's deployed in a way that interferes with the implementation. In an effort to decouple, [OWIN](https://owin.org/) was introduced to provide a cleaner way to use multiple frameworks together. OWIN provides a pipeline to add only the modules needed. The hosting environment takes a [Startup](xref:fundamentals/startup) function to configure services and the app's request pipeline. `Startup` registers a set of middleware with the application. For each request, the application calls each of the middleware components with the head pointer of a linked list to an existing set of handlers. Each middleware component can add one or more handlers to the request handling pipeline. This is accomplished by returning a reference to the handler that's the new head of the list. Each handler is responsible for remembering and invoking the next handler in the list. With ASP.NET Core, the entry point to an application is `Startup`, and you no longer have a dependency on *Global.asax*. When using OWIN with .NET Framework, use something like the following as a pipeline: | ||
|
|
||
| [!code-csharp[](~/migration/proper-to-2x/samples/webapi-owin.cs)] | ||
|
|
||
| This configures your default routes, and defaults to XmlSerialization over Json. Add other Middleware to this pipeline as needed (loading services, configuration settings, static files, etc.). | ||
|
|
||
| ASP.NET Core uses a similar approach, but doesn't rely on OWIN to handle the entry. Instead, that's done through the `Program.cs` `Main` method (similar to console applications) and `Startup` is loaded through there. | ||
|
|
||
| [!code-csharp[](~/migration/proper-to-2x/samples/program.cs)] | ||
|
|
||
| `Startup` must include a `Configure` method. In `Configure`, add the necessary middleware to the pipeline. In the following example (from the default web site template), extension methods configure the pipeline with support for: | ||
|
|
||
| - Error pages | ||
| - HTTP Strict Transport Security | ||
| - HTTP redirection to HTTPS | ||
| - ASP.NET Core MVC | ||
|
|
||
| [!code-csharp[](~/migration/proper-to-2x/samples/startup.cs)] | ||
|
|
||
| The host and application have been decoupled, which provides the flexibility of moving to a different platform in the future. | ||
|
|
||
| > [!NOTE] | ||
| > For a more in-depth reference to ASP.NET Core Startup and Middleware, see [Startup in ASP.NET Core](xref:fundamentals/startup) | ||
|
|
||
| ## Store configurations | ||
|
|
||
| ASP.NET supports storing settings. These setting are used, for example, to support the environment to which the applications were deployed. A common practice was to store all custom key-value pairs in the `<appSettings>` section of the *Web.config* file: | ||
|
|
||
| [!code-xml[](~/migration/proper-to-2x/samples/webconfig-sample.xml)] | ||
|
|
||
| Applications read these settings using the `ConfigurationManager.AppSettings` collection in the `System.Configuration` namespace: | ||
|
|
||
| [!code-csharp[](~/migration/proper-to-2x/samples/read-webconfig.cs)] | ||
|
|
||
| ASP.NET Core can store configuration data for the application in any file and load them as part of middleware bootstrapping. The default file used in the project templates is `appsettings.json`: | ||
|
|
||
| [!code-json[](~/migration/proper-to-2x/samples/appsettings-sample.json)] | ||
|
|
||
| Loading this file into an instance of `IConfiguration` inside your application is done in `Startup.cs`: | ||
|
|
||
| [!code-csharp[](~/migration/proper-to-2x/samples/startup-builder.cs)] | ||
|
|
||
| The app reads from `Configuration` to get the settings: | ||
|
|
||
| [!code-csharp[](~/migration/proper-to-2x/samples/read-appsettings.cs)] | ||
|
|
||
| There are extensions to this approach to make the process more robust, such as using [Dependency Injection](xref:fundamentals/dependency-injection) (DI) to load a service with these values. The DI approach provides a strongly-typed set of configuration objects. | ||
|
|
||
| ```csharp | ||
| // Assume AppConfiguration is a class representing a strongly-typed version of AppConfiguration section | ||
| services.Configure<AppConfiguration>(Configuration.GetSection("AppConfiguration")); | ||
| ``` | ||
|
|
||
| > [!NOTE] | ||
| > For a more in-depth reference to ASP.NET Core configuration, see [Configuration in ASP.NET Core](xref:fundamentals/configuration/index). | ||
|
|
||
| ## Native dependency injection | ||
|
|
||
| An important goal when building large, scalable applications is the loose coupling of components and services. [Dependency Injection](xref:fundamentals/dependency-injection) is a popular technique for achieving this, and it's a native component of ASP.NET Core. | ||
|
|
||
| In ASP.NET apps, developers rely on a third-party library to implement Dependency Injection. One such library is [Unity](https://github.com/unitycontainer/unity), provided by Microsoft Patterns & Practices. | ||
|
|
||
| An example of setting up Dependency Injection with Unity is implementing `IDependencyResolver` that wraps a `UnityContainer`: | ||
|
|
||
| [!code-csharp[](~/migration/proper-to-2x/samples/sample8.cs)] | ||
|
|
||
| Create an instance of your `UnityContainer`, register your service, and set the dependency resolver of `HttpConfiguration` to the new instance of `UnityResolver` for your container: | ||
|
|
||
| [!code-csharp[](~/migration/proper-to-2x/samples/sample9.cs)] | ||
|
|
||
| Inject `IProductRepository` where needed: | ||
|
|
||
| [!code-csharp[](~/migration/proper-to-2x/samples/sample5.cs)] | ||
|
|
||
| Because Dependency Injection is part of ASP.NET Core, you can add your service in the `ConfigureServices` method of `Startup.cs`: | ||
|
|
||
| [!code-csharp[](~/migration/proper-to-2x/samples/configure-services.cs)] | ||
|
|
||
| The repository can be injected anywhere, as was true with Unity. | ||
|
|
||
| > [!NOTE] | ||
| > For more information on dependency injection, see [Dependency injection](xref:fundamentals/dependency-injection). | ||
|
|
||
| ## Serve static files | ||
|
|
||
| An important part of web development is the ability to serve static, client-side assets. The most common examples of static files are HTML, CSS, Javascript, and images. These files need to be saved in the published location of the app (or CDN) and referenced so they can be loaded by a request. This process has changed in ASP.NET Core. | ||
|
|
||
| In ASP.NET, static files are stored in various directories and referenced in the views. | ||
|
|
||
| In ASP.NET Core, static files are stored in the "web root" (*<content root>/wwwroot*), unless configured otherwise. The files are loaded into the request pipeline by invoking the `UseStaticFiles` extension method from `Startup.Configure`: | ||
|
|
||
| [!code-csharp[](~/migration/proper-to-2x/samples/globalasax-sample.cs?highlight=3&name=snippet_ConfigureMethod)] | ||
|
|
||
| > [!NOTE] | ||
| > If targeting .NET Framework, install the NuGet package `Microsoft.AspNetCore.StaticFiles`. | ||
|
|
||
| For example, an image asset in the *wwwroot/images* folder is accessible to the browser at a location such as `http://<app>/images/<imageFileName>`. | ||
|
|
||
| > [!NOTE] | ||
| > For a more in-depth reference to serving static files in ASP.NET Core, see [Static files](xref:fundamentals/static-files). | ||
|
|
||
| ## Multi-value cookies | ||
|
|
||
| [Multi-value cookies](xref:System.Web.HttpCookie.Values) aren't supported in ASP.NET Core. Create one cookie per value. | ||
|
|
||
| ## Authentication cookies are not compressed in ASP.NET Core | ||
|
|
||
| [!INCLUDE[](~/includes/cookies-not-compressed.md)] | ||
|
|
||
| ## Partial app migration | ||
|
|
||
| One approach to partial app migration is to create an IIS sub-application and only move certain routes from ASP.NET 4.x to ASP.NET Core while preserving the URL structure the app. For example, consider the URL structure of the app from the *applicationHost.config* file: | ||
|
|
||
| ```xml | ||
| <sites> | ||
| <site name="Default Web Site" id="1" serverAutoStart="true"> | ||
| <application path="/"> | ||
| <virtualDirectory path="/" physicalPath="D:\sites\MainSite\" /> | ||
| </application> | ||
| <application path="/api" applicationPool="DefaultAppPool"> | ||
| <virtualDirectory path="/" physicalPath="D:\sites\netcoreapi" /> | ||
| </application> | ||
| <bindings> | ||
| <binding protocol="http" bindingInformation="*:80:" /> | ||
| <binding protocol="https" bindingInformation="*:443:" sslFlags="0" /> | ||
| </bindings> | ||
| </site> | ||
| ... | ||
| </sites> | ||
| ``` | ||
|
|
||
| Directory structure: | ||
|
|
||
| ``` | ||
| . | ||
| ├── MainSite | ||
| │ ├── ... | ||
| │ └── Web.config | ||
| └── NetCoreApi | ||
| ├── ... | ||
| └── web.config | ||
| ``` | ||
|
|
||
| ## [BIND] and Input Formatters | ||
|
|
||
| [Previous versions of ASP.NET](/aspnet/mvc/overview/getting-started/introduction/examining-the-edit-methods-and-edit-view) used the `[Bind]` attribute to protect against overposting attacks. [Input formatters](xref:mvc/models/model-binding#input-formatters) work differently in ASP.NET Core. The `[Bind]` attribute is no longer designed to prevent overposting when used with input formatters to parse JSON or XML. These attributes affect model binding when the source of data is form data posted with the `x-www-form-urlencoded` content type. | ||
|
|
||
| For apps that post JSON information to controllers and use JSON Input Formatters to parse the data, we recommend replacing the `[Bind]` attribute with a view model that matches the properties defined by the `[Bind]` attribute. | ||
| To generate the value for `HttpRequest.Url`, use `new Uri(this.AspNetCoreHttpRequest.GetEncodedUrl());` to avoid `Uri` misinterpreting the values. |
Contributor
Author
There was a problem hiding this comment.
@GREsau @Swimburger @loop-evgeny please review this section.
See #22352
Rick-Anderson
marked this pull request as ready for review
January 25, 2023 18:14
Rick-Anderson
commented
Jan 27, 2023
Comment on lines
+24
to
+36
| ASP.NET Core has the following URI decoding differences with ASP.NET Framework: | ||
|
|
||
| [.NET Core SDK 2.2 or later](https://dotnet.microsoft.com/download) | ||
| | ASCII | Encoded | ASP.NET Core | ASP.NET Framework | | ||
| | ------------- | ------------- | ------------- | ------------- | | ||
| | `\` | `%5C` | `/` | `\` | | ||
| | `/` | `%2F` | `%2F` | `/` | | ||
|
|
||
| ## Target frameworks | ||
| When decoding `%2F` on ASP.NET Core: | ||
|
|
||
| ASP.NET Core projects offer developers the flexibility of targeting .NET Core, .NET Framework, or both. See [Choosing between .NET Core and .NET Framework for server apps](/dotnet/standard/choosing-core-framework-server) to determine which target framework is most appropriate. | ||
| * The entire path gets unescaped except `%2F` because converting it to `/` would change the path structure. It can’t be decoded until the path is split into segments. | ||
| * See the [PathBase source](https://source.dot.net/#Microsoft.AspNetCore.Http.Abstractions/HttpRequest.cs,8d85f458c32cb4a5) for more information. | ||
|
|
||
| When targeting .NET Framework, projects need to reference individual NuGet packages. | ||
| To generate the value for `HttpRequest.Url`, use `new Uri(this.AspNetCoreHttpRequest.GetEncodedUrl());` to avoid `Uri` misinterpreting the values. |
Contributor
Author
Tratcher
reviewed
Jan 27, 2023
Tratcher
reviewed
Jan 27, 2023
Co-authored-by: Chris Ross <chrross@microsoft.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #21966 Fixes #27786 Fixes #22352 Fixes #21570 Fixes #27611
Internal review ULR