2

I am building a Web Application in ASP .NET 5 using Visual Studio 2015. I have created a solution with Data Access, Business, Services and User Interface layers. I have referenced the Services in User Interface layer.Since in MVC 6 both Web API and MVC fall under the same project template, it is necessary to have two different layers for services and UI or same project with different controllers is enough.?

And also in the UI project I have uncommented the following lines in Startup.cs

services.AddWebApiConventions();
routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");

While running the project, the home page comes up fine but when I click on links in the home page the url changes like this

http://localhost:45075/api/Home

And it gives error page saying that

AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied.
Sample.Services.Controllers.HomeController.Index
Sample.Services.Controllers.HomeController.About
Sample.Services.Controllers.HomeController.Contact
Sample.Services.Controllers.HomeController.Error

My understanding is that since I enabled Web Api conventions, it is going to the Home Controller of services which has route attribute

[Route("api/[controller]")]

If so how resolve this conflict by specifying namespace or some constraints while rendering views in the routes.

I am a beginner. Correct me if I am wrong.

1
  • 2
    Please try to ask one question per post, it makes providing useful answers a lot easier. That being said, you need to use conventions or explicit mapping, not both. Commented Feb 10, 2015 at 14:22

1 Answer 1

2

On MVC 6 you can have RESTful APIs as long as Views in the same project and even on a same controller.

One ways to set up routing for you application is to create a map on Startup.cs like this, with IApplicationBuilder:

Use IApplicationBuilder app

Then, you can specify the routes for each action:

Setting route for an Action

By setting a [HttpGet] (or any other HTTP verb) on the Action without parameters, it will create a route by convention, following you action's name. That way, you can solve many conflicts. It's also possible to put multiple actions of a same HTTP verb on one Controller.

And as stated above, please ask only one question per post.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.