The path in 'value must start with '/' (Parameter 'value')"

lesponce 176 Reputation points
2021-12-08T01:25:51.09+00:00

In my class Startup.cs I'm making a call to the following app.Map() as follow:

    app.Map(Config["CorpName"], MyConfig =>
    {
       MyConfig.UseMiddlewareYes("PolicyName);
    });

This is the class that is calling:

public static IApplicationBuilder UseMiddlewareYes(this IApplicationBuilder builder, string policyName)
{
return builder.UseMiddlewareYes(policyName);
}

By just make the call to the class, I get this error: The path in 'value must start with '/' (Parameter 'value')"

In the appsetting.json file the settings are configure like this:
"CorpName" : "https://mydomain.com",
"Settings":{
"flag1": "hello1",
"flag2: "hello2"
}

any help will be greatly appreciated.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,011 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 29,791 Reputation points Microsoft Vendor
    2021-12-08T09:32:53.197+00:00

    Hi @lesponce ,

    Welcome to Microsoft Q&A forum.

    I get this error: The path in 'value must start with '/' (Parameter 'value')"

    For this issue, please check the Branch the middleware pipeline:

    When Map is used, the matched path segments are removed from HttpRequest.Path and appended to HttpRequest.PathBase for each request. you can refer to the following sample code:

    private static void HandleMapTest1(IApplicationBuilder app)  
    {  
        app.Run(async context =>  
        {  
            await context.Response.WriteAsync("Map Test 1");  
        });  
    }  
      
    private static void HandleMapTest2(IApplicationBuilder app)  
    {  
        app.Run(async context =>  
        {  
            await context.Response.WriteAsync("Map Test 2");  
        });  
    }  
      
    public void Configure(IApplicationBuilder app)  
    {  
        app.Map("/map1", HandleMapTest1);  
      
        app.Map("/map2", HandleMapTest2);  
      
        app.Run(async context =>  
        {  
            await context.Response.WriteAsync("Hello from non-Map delegate. <p>");  
        });  
    }  
    

    As we can see in the Map method, the PathString should start with '/'. So, if you want to use the Map method, you have to change the path string to start with '/'.

    In the appsettings.json file, the value of CorpName should start with /, like this:

      "CorpName": "/https://mydomain.com",  
    

    You could check the following screenshot:

    156154-image.png

    Besides, from your code and description, you might want to get the request url and do something, right? If that is the case, you could consider using the MapWhen method, then, you can get the request url from the HttpContext. refer to the following code:

    private static void HandleBranch(IApplicationBuilder app)  
    {  
        app.Run(async context =>  
        {  
            var branchVer = context.Request.Query["branch"];  
            await context.Response.WriteAsync($"Branch used = {branchVer}");  
        });  
    }  
    
    public void Configure(IApplicationBuilder app)  
    {  
        app.MapWhen(context => context.Request.Query.ContainsKey("branch"),  
                               HandleBranch);  
    
        app.Run(async context =>  
        {  
            await context.Response.WriteAsync("Hello from non-Map delegate. <p>");  
        });  
    }  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion