Skip to content

Commit fa2fd21

Browse files
authored
Add Uri StringSyntaxAttribute syntaxes (#44570)
1 parent e35f2ea commit fa2fd21

38 files changed

+199
-95
lines changed

src/Components/Components/src/NavigationManager.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Buffers;
5+
using System.Diagnostics.CodeAnalysis;
56
using Microsoft.AspNetCore.Components.Routing;
67

78
namespace Microsoft.AspNetCore.Components;
@@ -103,7 +104,7 @@ protected set
103104
/// <param name="uri">The destination URI. This can be absolute, or relative to the base URI
104105
/// (as returned by <see cref="BaseUri"/>).</param>
105106
/// <param name="forceLoad">If true, bypasses client-side routing and forces the browser to load the new page from the server, whether or not the URI would normally be handled by the client-side router.</param>
106-
public void NavigateTo(string uri, bool forceLoad) // This overload is for binary back-compat with < 6.0
107+
public void NavigateTo([StringSyntax(StringSyntaxAttribute.Uri)] string uri, bool forceLoad) // This overload is for binary back-compat with < 6.0
107108
=> NavigateTo(uri, forceLoad, replace: false);
108109

109110
/// <summary>
@@ -113,7 +114,7 @@ protected set
113114
/// (as returned by <see cref="BaseUri"/>).</param>
114115
/// <param name="forceLoad">If true, bypasses client-side routing and forces the browser to load the new page from the server, whether or not the URI would normally be handled by the client-side router.</param>
115116
/// <param name="replace">If true, replaces the current entry in the history stack. If false, appends the new entry to the history stack.</param>
116-
public void NavigateTo(string uri, bool forceLoad = false, bool replace = false)
117+
public void NavigateTo([StringSyntax(StringSyntaxAttribute.Uri)] string uri, bool forceLoad = false, bool replace = false)
117118
{
118119
AssertInitialized();
119120

@@ -139,7 +140,7 @@ public void NavigateTo(string uri, bool forceLoad = false, bool replace = false)
139140
/// <param name="uri">The destination URI. This can be absolute, or relative to the base URI
140141
/// (as returned by <see cref="BaseUri"/>).</param>
141142
/// <param name="options">Provides additional <see cref="NavigationOptions"/>.</param>
142-
public void NavigateTo(string uri, NavigationOptions options)
143+
public void NavigateTo([StringSyntax(StringSyntaxAttribute.Uri)] string uri, NavigationOptions options)
143144
{
144145
AssertInitialized();
145146
NavigateToCore(uri, options);
@@ -155,7 +156,7 @@ public void NavigateTo(string uri, NavigationOptions options)
155156
// already override this, so the framework needs to keep using it for the cases when only pre-6.0 options are used.
156157
// However, for anyone implementing a new NavigationManager post-6.0, we don't want them to have to override this
157158
// overload any more, so there's now a default implementation that calls the updated overload.
158-
protected virtual void NavigateToCore(string uri, bool forceLoad)
159+
protected virtual void NavigateToCore([StringSyntax(StringSyntaxAttribute.Uri)] string uri, bool forceLoad)
159160
=> NavigateToCore(uri, new NavigationOptions { ForceLoad = forceLoad });
160161

161162
/// <summary>
@@ -164,7 +165,7 @@ protected virtual void NavigateToCore(string uri, bool forceLoad)
164165
/// <param name="uri">The destination URI. This can be absolute, or relative to the base URI
165166
/// (as returned by <see cref="BaseUri"/>).</param>
166167
/// <param name="options">Provides additional <see cref="NavigationOptions"/>.</param>
167-
protected virtual void NavigateToCore(string uri, NavigationOptions options) =>
168+
protected virtual void NavigateToCore([StringSyntax(StringSyntaxAttribute.Uri)] string uri, NavigationOptions options) =>
168169
throw new NotImplementedException($"The type {GetType().FullName} does not support supplying {nameof(NavigationOptions)}. To add support, that type should override {nameof(NavigateToCore)}(string uri, {nameof(NavigationOptions)} options).");
169170

170171
/// <summary>

src/Components/WebAssembly/Server/src/TargetPickerUi.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using System.Linq;
56
using System.Net;
67
using System.Net.Http;
@@ -30,7 +31,7 @@ public class TargetPickerUi
3031
/// </summary>
3132
/// <param name="debugProxyUrl">The debug proxy url.</param>
3233
/// <param name="devToolsHost">The dev tools host.</param>
33-
public TargetPickerUi(string debugProxyUrl, string devToolsHost)
34+
public TargetPickerUi([StringSyntax(StringSyntaxAttribute.Uri)] string debugProxyUrl, string devToolsHost)
3435
{
3536
_debugProxyUrl = debugProxyUrl;
3637
_browserHost = devToolsHost;

src/Components/WebAssembly/WebAssembly.Authentication/src/NavigationManagerExtensions.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication;
57

68
/// <summary>
@@ -17,7 +19,7 @@ public static class NavigationManagerExtensions
1719
/// </remarks>
1820
/// <param name="manager">The <see cref="NavigationManager"/>.</param>
1921
/// <param name="logoutPath">The path to navigate to.</param>
20-
public static void NavigateToLogout(this NavigationManager manager, string logoutPath) =>
22+
public static void NavigateToLogout(this NavigationManager manager, [StringSyntax(StringSyntaxAttribute.Uri, UriKind.Relative)] string logoutPath) =>
2123
manager.NavigateToLogout(logoutPath, null);
2224

2325
/// <summary>
@@ -30,7 +32,7 @@ public static void NavigateToLogout(this NavigationManager manager, string logou
3032
/// <param name="manager">The <see cref="NavigationManager"/>.</param>
3133
/// <param name="logoutPath">The path to navigate too.</param>
3234
/// <param name="returnUrl">The url to redirect the user to after logging out.</param>
33-
public static void NavigateToLogout(this NavigationManager manager, string logoutPath, string returnUrl)
35+
public static void NavigateToLogout(this NavigationManager manager, [StringSyntax(StringSyntaxAttribute.Uri, UriKind.Relative)] string logoutPath, [StringSyntax(StringSyntaxAttribute.Uri)] string returnUrl)
3436
{
3537
manager.NavigateTo(logoutPath, new NavigationOptions
3638
{
@@ -52,7 +54,7 @@ public static void NavigateToLogout(this NavigationManager manager, string logou
5254
/// <param name="manager">The <see cref="NavigationManager"/>.</param>
5355
/// <param name="loginPath">The path to the login url.</param>
5456
/// <param name="request">The <see cref="InteractiveRequestOptions"/> containing the authorization details.</param>
55-
public static void NavigateToLogin(this NavigationManager manager, string loginPath, InteractiveRequestOptions request)
57+
public static void NavigateToLogin(this NavigationManager manager, [StringSyntax(StringSyntaxAttribute.Uri, UriKind.Relative)] string loginPath, InteractiveRequestOptions request)
5658
{
5759
manager.NavigateTo(loginPath, new NavigationOptions
5860
{
@@ -69,7 +71,7 @@ public static void NavigateToLogin(this NavigationManager manager, string loginP
6971
/// </remarks>
7072
/// <param name="manager">The <see cref="NavigationManager"/>.</param>
7173
/// <param name="loginPath">The path to the login url.</param>
72-
public static void NavigateToLogin(this NavigationManager manager, string loginPath)
74+
public static void NavigateToLogin(this NavigationManager manager, [StringSyntax(StringSyntaxAttribute.Uri, UriKind.Relative)] string loginPath)
7375
{
7476
manager.NavigateToLogin(
7577
loginPath,

src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticatorViewCore.Log.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using Microsoft.Extensions.Logging;
56

67
namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication;
@@ -19,7 +20,7 @@ private static partial class Log
1920
public static partial void LoginRequiresRedirect(ILogger logger);
2021

2122
[LoggerMessage(4, LogLevel.Debug, "Navigating to {Url}.", EventName = nameof(NavigatingToUrl))]
22-
public static partial void NavigatingToUrl(ILogger logger, string url);
23+
public static partial void NavigatingToUrl(ILogger logger, [StringSyntax(StringSyntaxAttribute.Uri)] string url);
2324

2425
[LoggerMessage(5, LogLevel.Debug, "Raising LoginCompleted event.", EventName = nameof(InvokingLoginCompletedCallback))]
2526
public static partial void InvokingLoginCompletedCallback(ILogger logger);

src/Components/WebAssembly/WebAssembly.Authentication/src/Services/AccessTokenResult.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication;
57

68
/// <summary>
@@ -17,7 +19,7 @@ public class AccessTokenResult
1719
/// <param name="token">The <see cref="AccessToken"/> in case it was successful.</param>
1820
/// <param name="redirectUrl">The redirect uri to go to for provisioning the token.</param>
1921
[Obsolete("Use the AccessTokenResult(AccessTokenResultStatus, AccessToken, string, InteractiveRequestOptions)")]
20-
public AccessTokenResult(AccessTokenResultStatus status, AccessToken token, string redirectUrl)
22+
public AccessTokenResult(AccessTokenResultStatus status, AccessToken token, [StringSyntax(StringSyntaxAttribute.Uri)] string redirectUrl)
2123
{
2224
Status = status;
2325
_token = token;
@@ -31,7 +33,7 @@ public AccessTokenResult(AccessTokenResultStatus status, AccessToken token, stri
3133
/// <param name="token">The <see cref="AccessToken"/> in case it was successful.</param>
3234
/// <param name="interactiveRequestUrl">The redirect uri to go to for provisioning the token with <see cref="NavigationManagerExtensions.NavigateToLogin(NavigationManager, string, InteractiveRequestOptions)"/>.</param>
3335
/// <param name="interactiveRequest">The <see cref="InteractiveRequestOptions"/> containing the parameters for the interactive authentication.</param>
34-
public AccessTokenResult(AccessTokenResultStatus status, AccessToken token, string interactiveRequestUrl, InteractiveRequestOptions interactiveRequest)
36+
public AccessTokenResult(AccessTokenResultStatus status, AccessToken token, [StringSyntax(StringSyntaxAttribute.Uri)] string interactiveRequestUrl, InteractiveRequestOptions interactiveRequest)
3537
{
3638
Status = status;
3739
_token = token;

src/Components/WebAssembly/WebAssembly.Authentication/src/Services/AuthorizationMessageHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using System.Linq;
56
using System.Net.Http;
67
using System.Net.Http.Headers;
@@ -94,7 +95,7 @@ await _provider.RequestAccessToken(_tokenOptions) :
9495
public AuthorizationMessageHandler ConfigureHandler(
9596
IEnumerable<string> authorizedUrls,
9697
IEnumerable<string> scopes = null,
97-
string returnUrl = null)
98+
[StringSyntax(StringSyntaxAttribute.Uri)] string returnUrl = null)
9899
{
99100
if (_authorizedUris != null)
100101
{

src/Components/WebView/WebView/src/WebViewManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using System.Reflection;
56
using Microsoft.AspNetCore.Components.Web;
67
using Microsoft.AspNetCore.StaticWebAssets;
@@ -62,7 +63,7 @@ public WebViewManager(IServiceProvider provider, Dispatcher dispatcher, Uri appB
6263
/// client-side routing.
6364
/// </summary>
6465
/// <param name="url">The URL, which may be absolute or relative to the application root.</param>
65-
public void Navigate(string url)
66+
public void Navigate([StringSyntax(StringSyntaxAttribute.Uri)] string url)
6667
=> NavigateCore(new Uri(_appBaseUri, url));
6768

6869
/// <summary>

src/DefaultBuilder/src/WebApplication.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using Microsoft.AspNetCore.Hosting;
56
using Microsoft.AspNetCore.Hosting.Server;
67
using Microsoft.AspNetCore.Hosting.Server.Features;
@@ -142,7 +143,7 @@ public Task StopAsync(CancellationToken cancellationToken = default) =>
142143
/// <returns>
143144
/// A <see cref="Task"/> that represents the entire runtime of the <see cref="WebApplication"/> from startup to shutdown.
144145
/// </returns>
145-
public Task RunAsync(string? url = null)
146+
public Task RunAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? url = null)
146147
{
147148
Listen(url);
148149
return HostingAbstractionsHostExtensions.RunAsync(this);
@@ -152,7 +153,7 @@ public Task RunAsync(string? url = null)
152153
/// Runs an application and block the calling thread until host shutdown.
153154
/// </summary>
154155
/// <param name="url">The URL to listen to if the server hasn't been configured directly.</param>
155-
public void Run(string? url = null)
156+
public void Run([StringSyntax(StringSyntaxAttribute.Uri)] string? url = null)
156157
{
157158
Listen(url);
158159
HostingAbstractionsHostExtensions.Run(this);

src/DefaultBuilder/src/WebHost.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static IWebHost Start(RequestDelegate app) =>
3838
/// <param name="url">The URL the hosted application will listen on.</param>
3939
/// <param name="app">A delegate that handles requests to the application.</param>
4040
/// <returns>A started <see cref="IWebHost"/> that hosts the application.</returns>
41-
public static IWebHost Start(string url, RequestDelegate app)
41+
public static IWebHost Start([StringSyntax(StringSyntaxAttribute.Uri)] string url, RequestDelegate app)
4242
{
4343
var startupAssemblyName = app.GetMethodInfo().DeclaringType!.Assembly.GetName().Name;
4444
return StartWith(url: url, configureServices: null, app: appBuilder => appBuilder.Run(app), applicationName: startupAssemblyName);
@@ -60,7 +60,7 @@ public static IWebHost Start(Action<IRouteBuilder> routeBuilder) =>
6060
/// <param name="url">The URL the hosted application will listen on.</param>
6161
/// <param name="routeBuilder">A delegate that configures the router for handling requests to the application.</param>
6262
/// <returns>A started <see cref="IWebHost"/> that hosts the application.</returns>
63-
public static IWebHost Start(string url, Action<IRouteBuilder> routeBuilder)
63+
public static IWebHost Start([StringSyntax(StringSyntaxAttribute.Uri)] string url, Action<IRouteBuilder> routeBuilder)
6464
{
6565
var startupAssemblyName = routeBuilder.GetMethodInfo().DeclaringType!.Assembly.GetName().Name;
6666
return StartWith(url, services => services.AddRouting(), appBuilder => appBuilder.UseRouter(routeBuilder), applicationName: startupAssemblyName);
@@ -82,7 +82,7 @@ public static IWebHost StartWith(Action<IApplicationBuilder> app) =>
8282
/// <param name="url">The URL the hosted application will listen on.</param>
8383
/// <param name="app">The delegate that configures the <see cref="IApplicationBuilder"/>.</param>
8484
/// <returns>A started <see cref="IWebHost"/> that hosts the application.</returns>
85-
public static IWebHost StartWith(string url, Action<IApplicationBuilder> app) =>
85+
public static IWebHost StartWith([StringSyntax(StringSyntaxAttribute.Uri)] string url, Action<IApplicationBuilder> app) =>
8686
StartWith(url: url, configureServices: null, app: app, applicationName: null);
8787

8888
private static IWebHost StartWith(string? url, Action<IServiceCollection>? configureServices, Action<IApplicationBuilder> app, string? applicationName)

src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public static IWebHostBuilder UseWebRoot(this IWebHostBuilder hostBuilder, strin
137137
/// <param name="hostBuilder">The <see cref="IWebHostBuilder"/> to configure.</param>
138138
/// <param name="urls">The urls the hosted application will listen on.</param>
139139
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
140-
public static IWebHostBuilder UseUrls(this IWebHostBuilder hostBuilder, params string[] urls)
140+
public static IWebHostBuilder UseUrls(this IWebHostBuilder hostBuilder, [StringSyntax(StringSyntaxAttribute.Uri)] params string[] urls)
141141
{
142142
if (urls == null)
143143
{
@@ -187,7 +187,7 @@ public static IWebHostBuilder UseShutdownTimeout(this IWebHostBuilder hostBuilde
187187
/// <param name="hostBuilder">The <see cref="IWebHostBuilder"/> to start.</param>
188188
/// <param name="urls">The urls the hosted application will listen on.</param>
189189
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
190-
public static IWebHost Start(this IWebHostBuilder hostBuilder, params string[] urls)
190+
public static IWebHost Start(this IWebHostBuilder hostBuilder, [StringSyntax(StringSyntaxAttribute.Uri)] params string[] urls)
191191
{
192192
var host = hostBuilder.UseUrls(urls).Build();
193193
host.StartAsync(CancellationToken.None).GetAwaiter().GetResult();

src/Http/Http.Abstractions/src/HttpResponse.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using System.IO.Pipelines;
56

67
namespace Microsoft.AspNetCore.Http;
@@ -126,15 +127,15 @@ public abstract class HttpResponse
126127
/// </summary>
127128
/// <param name="location">The URL to redirect the client to. This must be properly encoded for use in http headers
128129
/// where only ASCII characters are allowed.</param>
129-
public virtual void Redirect(string location) => Redirect(location, permanent: false);
130+
public virtual void Redirect([StringSyntax(StringSyntaxAttribute.Uri)] string location) => Redirect(location, permanent: false);
130131

131132
/// <summary>
132133
/// Returns a redirect response (HTTP 301 or HTTP 302) to the client.
133134
/// </summary>
134135
/// <param name="location">The URL to redirect the client to. This must be properly encoded for use in http headers
135136
/// where only ASCII characters are allowed.</param>
136137
/// <param name="permanent"><c>True</c> if the redirect is permanent (301), otherwise <c>false</c> (302).</param>
137-
public abstract void Redirect(string location, bool permanent);
138+
public abstract void Redirect([StringSyntax(StringSyntaxAttribute.Uri)] string location, bool permanent);
138139

139140
/// <summary>
140141
/// Starts the response by calling OnStarting() and making headers unmodifiable.

src/Http/Http.Extensions/src/UriHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Buffers;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Runtime.CompilerServices;
67
using System.Text;
78

@@ -105,7 +106,7 @@ public static string BuildAbsolute(
105106
/// <param name="query">The query, if any.</param>
106107
/// <param name="fragment">The fragment, if any.</param>
107108
public static void FromAbsolute(
108-
string uri,
109+
[StringSyntax(StringSyntaxAttribute.Uri)] string uri,
109110
out string scheme,
110111
out HostString host,
111112
out PathString path,

0 commit comments

Comments
 (0)