0

I want to remove controller name from URL for specific Controller.

My Controller name is Product

I found some link to do this

Routing with and without controller name

MVC Routing without controller

But all the above links done in route config file. and those are affecting other controller too. I want to do it using Attribute Routing.

Can it is possible? As I want to do this for only Product controller.

I have tried to do it on action like this

[Route("Sample/{Name}")]

but it is not working.

2 Answers 2

1

Gabriel's answer is right, however, it can be a bit misleading since you're asking for MVC and that answer is for Web API.

In any case, what you want is to put the annotation over the class definition instead of an action method. MVC example would be like:

[RoutePrefix("SomethingOtherThanProduct")]
public class ProductController : Controller
{
      public ActionResult Index()
      {
           ...
           return View();
      }
}

I'm also dropping this as an answer since you may find the following article helpful: [Attribute] Routing in ASP.NET MVC 5 / WebAPI 2

Sign up to request clarification or add additional context in comments.

Comments

0

Make sure you set the RoutePrefix attribute on the whole controller class, as well as using the Route attribute on the action.

[RoutePrefix("notproducts")]
public class ProductsController : ApiController
{
    [Route("")]
    public IEnumerable<Product> Get() { ... }
}

Comments

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.