0

New to the world of Web API and I can't seem to get any custom view to display. I've done the basics, made a method in my controller that seems to be linked corrected where I'm calling it. I'm more familiar with MVC so I'm wondering am I missing something basic here? The view always returns an error can't be accessed for me.

The ActionResult, in the generated HomeController:

[HttpGet]
public ActionResult SpotDetails()
{
    ViewBag.Title = "Spot Details";
    return View();
}

The link to call it in the menu:

<ul class="nav navbar-nav">
    <li>@Html.ActionLink("SHOR //", "Index", "Home", new {area = ""}, null)</li>
    <li>@Html.ActionLink("Index", "Index", "Home", new {area = ""}, null)</li>
    <li>@Html.ActionLink("Spot Profile", "SpotDetails", "Home", new {area = ""}, null)</li>
    <li>@Html.ActionLink("API", "Index", "Help", new {area = ""}, null)</li>
</ul>

I should say the links to the index and the api help pages work perfectly.

This is my RouteConfig in case it's at fault, I haven't touched it at all:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

And finally my WebApiConfig:

public static void Register(HttpConfiguration config)
{
    // Web API configuration and services
    config.Formatters.Remove(config.Formatters.XmlFormatter);
    config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling =
        Newtonsoft.Json.ReferenceLoopHandling.Ignore;

    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}

Edit - the error message when the link is clicked:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its >dependencies) could have been removed, had its name changed, or is temporarily >unavailable. Please review the following URL and make sure that it is spelled >correctly.

Requested URL: /Home/SpotDetails

3
  • What are the details of the error? Commented Apr 2, 2016 at 6:04
  • @StephenMuecke Added the exact error Commented Apr 2, 2016 at 6:09
  • Looks good to me. You sure that SpotDetails.cshtml is in /Views/Home/ and it is spelled exactly the same? Commented Apr 2, 2016 at 8:26

1 Answer 1

1

Most likely this is an issue with your startup configuration. Routes must be registered in order from most specific to least specific. You have accounted for that within your RouteConfig.RegisterRoutes and WebApiConfig.Register methods. However, you must also call WebApiConfig.Register before RouteConfig.RegisterRoutes or your Default route will take precedence over any of the Web API routes.

WebApiConfig.Register(GlobalConfiguration.Configuration);
RouteConfig.RegisterRoutes(RouteTable.Routes);
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.