Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If my Web API test application (created as a Web API app) is working perfectly from the URL, what might be a reason why the exact same code copied into a straight MVC application fails to route? Is there something essential missing if I did not select WebAPI as I was creating the MVC application? I'm trying to add a WebAPI (which retrieves data from a SQL Server stored procedure) to an existing MVC application. I went to the Model Browser and made sure and did the Function Import to bring in the fields, as I did in the test app.

I'm not using a separate project for the WebAPI; it's all in one project, so there's no issue with the port number. I've been stuck on this for a few days now. I've deleted and re-created the Model in the MVC app numerous times. The test app and the MVC app with the Web API added have the exact same APIController and the same WebApiConfig.cs file. When I enter the URL with the data in the browser, it works in the test app, giving the proper response to the browser, but not in the MVC app.

The response to the browser when using the MVC app is:

Server Error in '/' Application
The resource cannot be found.

If anybody has an idea what might be the cause, please let me know. Thanks.

--------------------------------------------------------------------------

WebApiConfig.cs

// Web API routes
config.MapHttpAttributeRoutes();

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


Controller
public class CompletedRoundsController : ApiController
{
// GET
public IEnumerable<string> Get(int ID, string AsmtType)
{
using (WebAPIEntities db = new WebAPIEntities())
{
List<string> R = db.spCurrentRounds(ID, AsmtType).Select(x => x.Completed).ToList();
return R;
}
}

What I have tried:

http://localhost:58398/api/CompletedRounds?ID=1&AsmtType=ABC
Posted
Updated 12-Apr-18 21:06pm

1 solution

I found the solution. Although the "WebAPI Controller" scaffolding automatically added the WebApiConfig.cs file, it did not make sure that the following two lines exist in the Global.asax file, so I added them and it works great now.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;

namespace CCOutcomesWeb
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900