Startup.cs-“值"中的路径必须以"/"开头 [英] Startup.cs - The path in 'value' must start with '/'

查看:333
本文介绍了Startup.cs-“值"中的路径必须以"/"开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio 2017中创建了一个新的.NET Core MVC应用程序,并启用了多租户身份验证.
我已经完成了配置(ClientId,Author等),但是在调试应用程序时,Startup.cs中有一个异常,特别是app.useOpenIdConnectAuthentication方法.

给出的例外是

System.ArgumentException:值"中的路径必须以"/"开头.

在C#和.NET Core方面,我还是个新手,所以我不确定是否缺少明显的东西.主要的症结在于调试器使用参数值"所引用的内容,因为我在代码中看不到任何提及.除了将配置项添加到appsettings.json中之外,Visual Studio生成的默认模板之外没有任何更改.

解决方案

由于该问题中没有代码,因此我将尝试给出一个一般性的答案.
当您使用此重载PathString.FromUriComponent(string)并且字符串不是以/字符

开头时,将出现此异常.

例如,以下代码将引发异常:

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

要修复先前的异常,您可以这样编写

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

当然,这将是一个相对路径.

如果要使用绝对路径(而不是使用/开头的字符串),则必须使用此方法的另一个重载,该重载将Uri对象作为参数,而不是string

这是一个示例

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

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

The exception given is

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

I'm a bit of a novice when it comes to C# and .NET Core, so I'm not sure whether I'm missing something obvious. The main sticking point is what the debugger is referring to with the parameter 'value' as I can't see any mention of it in the code. There are no changes beyond the default template generated by visual studio, other than adding the configuration items to the appsettings.json.

解决方案

As there is no code in the question, so I will try to make a general answer as possible.
This exception appears when you use this overload PathString.FromUriComponent(string) and the string 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 it like this

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

and of course, this will be a relative path.

In case you wanted to use an absolute path, (and not start your string with /), then you must use another overload of this method which takes 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/"))

这篇关于Startup.cs-“值"中的路径必须以"/"开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆