0

I'm new to WebAPI... I know how to create an MVC View retuning from a request when not using the WebAPI.

Typically, in the _Layout.cshtml page, you have an "Index" controller action method that is executed for a particular controller and the Index View comes up fine.

I've tried several things, but cannot get the default View to come up when using a WebAPI controller. For instance, I have a "GetCategories" action method inside the WebAPI controller.

Instead of the non-WebAPI controller method where the "Index" route works (via my _Layout.cshtml page), I don't know what to place inside here where a default view would come up.

<li>@Html.ActionLink("Categories", "GetCategories", "Categories")</li>

Can someone please point me in the right direction here?

Note that I changed the action method to "GetCategories" to agree with what I have in the WebAPI controller, but obviously, like I mentioned, doesn't come up with the corresponding View name that I have for it under the Views/Categories folder (GetCategories).

I get the error "Requested URL: /Categories/GetCategories" not found.

1
  • There is no view with WebApi. Probably your route definition(s) for WebApi are not complete or not called. Commented Jan 11, 2015 at 18:36

1 Answer 1

1

The dafault routing for WebAPI do not take method names as parts of the URL.

You shoud use just "/Categories". As long as You will call "your.server.address/Categories" with HTTP GET method any method inside CategoriesController with name starting with "Get" will be called. So You should use:

<li>@Html.ActionLink("Categories", null, "Categories")</li>

But be advised that clicking the link will display (if You used default settings) whatever You are returning as JSON in Your browser.

You can read more about Asp.NET WebAPI routing here

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

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.