Startup.cs - the path in 'value' should start with '/'

I created a new .NET Core MVC application in Visual Studio 2017 and enabled multi-level authentication.
I have completed the configuration (ClientId, Authority, etc.), but when I debug the application, there is an exception in Startup.cs, in particular the app.useOpenIdConnectAuthentication method.

The exception is

System.ArgumentException: The path in 'value' must begin with '/'.

I'm starting a little when it comes to C # and .NET Core, so I'm not sure if I am missing something obvious. The main anchor point is that the debugger refers to the value parameter, since I don't see any mention of this in the code. Behind the default template created by visual studio, there are no changes, except for adding configuration items to appsettings.json.

+7
source share
2 answers

Since there is no code in the question, I will try to make a general answer as much as possible.
This exception occurs when you use this overload of PathString.FromUriComponent(string) , and the line does not start with the / character

so, for example, the following code will throw an exception:

 PathString.FromUriComponent("controllerName/actionName"); // throw exception 

and to fix the previous exception, you can write like this

 PathString.FromUriComponent("/controllerName/actionName"); // working, but as relative path 

and of course it will be a relative path.

If you want to use an absolute path (rather than starting a line with / ), then you must use another overload of this method, which takes a Uri object as a parameter instead of string

here is an example

 // use an absolute path PathString.FromUriComponent(new Uri("https://localhost:8000/controller/action/")) 
+4
source

I also get the same exception, but with a different UseExceptionHandler method:

The error was:

ArgumentException: The path in 'value' must start with '/'. Nombre del parámetro: value Microsoft.AspNetCore.Http.PathString..ctor (string value) Microsoft.AspNetCore.Builder.ExceptionHandlerExtensions.UseExceptionHandler (IApplicationBuilder app, string errorHandlingPath)

Fixed by replacement

 app.UseExceptionHandler("Home/Error"); 

with

 app.UseExceptionHandler("/Home/Error"); 

note the addition of the / character at the beginning of the line.

+1
source

Source: https://habr.com/ru/post/1263277/


All Articles