0

I have one ASP.NET Core 3.1.0 web app and I am struggling to understand the url exposed by asp.net core.

I have one single controller

[Route("accounts")]
public class AccountsController : BaseController
{
    public AccountsController()
    {
    }

    [HttpGet("{test}")]
    public string DefaultMethod()
    {
        return "Hello";
    }
}        

In my Startup.cs, I am using UsePathBase as

app.UsePathBase("/account-api");

When I start the app, I can access the method as

http://localhost:5000/account-api/accounts/test and it is fine

But I can also access it via http://localhost:5000/accounts/test which I want to restrict.

How can I restrict this?

1
  • You may want to decorate your controller with [Route("account-api/[controller]")] to restrict Commented Aug 5, 2020 at 16:26

1 Answer 1

1

try this

         app.UseMvc(routes =>
         {
            routes.MapRoute(
               name: "default",
               template: "account-api/{controller}/{action}/{id?}");
         });
Sign up to request clarification or add additional context in comments.

3 Comments

I am using Web API template and without MVC so don't think this should be used in my case
why not? it's completely fine to use MVC model in WebAPIs. but you have app.UseRouter option too. instead of useMvc. @BhavikSuthar
I have marked this as answered but went with app.UseRouter as UseMVC gave the wrong impression on the codebase

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.