Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Web API MVC Controller - not finding correct controller
Message
From
02/07/2014 15:07:21
 
 
To
02/07/2014 12:29:56
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 4.0
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01602962
Message ID:
01603131
Views:
49
>>>>Hi,
>>>>
>>>>I am having a problem with getting the correct controller to run.
>>>>
>>>>I have the following setup:
>>>>
>>>>In my web project, in the root of the Controllers folder I have RunSheetDetailsController.cs:
>>>>
>>>>
using IBCPackTrack.Data;
>>>>using IBCPackTrack.Models;
>>>>using IBCPackTrack.Web.Filters;
>>>>using IBCPackTrack.Web.ViewModels;
>>>>using System;
>>>>using System.IO;
>>>>using System.Linq;
>>>>using System.Web;
>>>>using System.Web.Mvc;
>>>>
>>>>namespace IBCPackTrack.Web.Controllers
>>>>{
>>>>    public class RunSheetDetailsController : Controller
>>>>    {
>>>>        private ApplicationUnit _unit = new ApplicationUnit();
>>>>
>>>>        //[AllowAnonymous]
>>>>        public ActionResult Index()
>>>>        {
>>>>            RunSheetDetailsListViewModel vm = new RunSheetDetailsListViewModel();
>>>>            var query = this._unit.RunSheetDetails.GetAll(); //.GetAll().OrderBy(rnh => rnh.Route.rte_name);
>>>>            vm.RunSheetDetails = query.ToList();
>>>>
>>>>            return View("Index", vm);
>>>>        }
>>>>
>>>>        [ActionName("Edit")]
>>>>        public ActionResult Get(Guid id)
>>>>        {
>>>>            RunSheetDetailViewModel vm = new RunSheetDetailViewModel();
>>>>
>>>>            vm.RunSheetDetail = this._unit.RunSheetDetails.GetById(id);
>>>>
>>>>            if (vm.RunSheetDetail != null)
>>>>            {
>>>>                return View("RunSheetDetail", vm);
>>>>            }
>>>>
>>>>        }
>>>>}
>>>>
>>>>Controllers has a sub-folder called "api" where I have my RunSheetDetailsAPIController.cs:
>>>>
>>>>
using System;
>>>>using System.Collections.Generic;
>>>>using System.Linq;
>>>>using System.Net;
>>>>using System.Net.Http;
>>>>using System.Web.Http;
>>>>
>>>>using IBCPackTrack.Data;
>>>>using IBCPackTrack.Models;
>>>>using System.Data.Entity.Infrastructure;
>>>>
>>>>namespace IBCPackTrack.Web.Controllers
>>>>{
>>>>    //[Authorize]
>>>>    public class RunSheetDetailsAPIController : ApiController
>>>>    {
>>>>        private ApplicationUnit _unit = new ApplicationUnit();
>>>>
>>>>        [HttpGet]
>>>>        //[AllowAnonymous]
>>>>        public IEnumerable<RunSheetDetail> Get()
>>>>        {
>>>>            return this._unit.RunSheetDetails.GetAll();
>>>>        }
>>>>
>>>>        [HttpGet]
>>>>        public IEnumerable<RunSheetDetail> Get(Guid id)
>>>>        {
>>>>            return this._unit.RunSheetDetails.GetByRunSheetId(id);
>>>>        }
>>>>    }
>>>>}
>>>>
>>>>The view of the page that is supposed to make this call has this code in it:
>>>>
>>>>
<a class="btn btn-primary btn-mini" data-bind="attr: {href: '/runsheetdetails/' + rnh_pk}">Select</a>
>>>>
>>>>My WebApiConfig has this code:
>>>>
>>>>
            config.Routes.MapHttpRoute(
>>>>                name: "APIrnd",
>>>>                routeTemplate: "api/runsheetdetails/{id}",
>>>>                defaults: new
>>>>                {
>>>>                    controller = "RunSheetDetailsAPI",
>>>>                    id = RouteParameter.Optional
>>>>                });
>>>>
>>>>            config.Routes.MapHttpRoute(
>>>>                name: "DefaultApi",
>>>>                routeTemplate: "api/{controller}/{id}",
>>>>                defaults: new { id = RouteParameter.Optional }
>>>>
>>>>What happens when I click on the link defined above, the code runs through the RunSheetDetailsAPIController rather than the RunSheetDetailsController, so all I get back is the json, whereas I want the index.cshml to display the json in the view.
>>>>
>>>>Any ideas what I'm not seeing here?
>>>
>>>Its your routing. First, your first route has runsheetdetails in the template, and no controller. When if finds a url matching this template it is going to use the default controller. Second, you are missing a route for your MVC controllers. The only routes you have include api as part of the template. Also, MVC routes usually include an action component as part of the template. Finally, I'm guessing that your RunSheetDetailsController isn't matching any methods for your action. Your index action doesn't take parameters, and the get method is mapped to the action of edit. Being mapped to a different action may prevent it from being used implicitly.
>>
>>Thanks Rob,
>>
>>I had worked out the index action/parameter problem and got that aspect to work, but could you explain these statements you made please?
>>
>>1. "your first route has runsheetdetails in the template, and no controller"
>>
>>I thought that this: controller = "RunSheetDetailsAPI" set the controller.
>>
>>2. "you are missing a route for your MVC controllers"
>>
>>I thought that this routeTemplate: "api/runsheetdetails/{id}" did that.
>>
>>Obviously I'm not understanding the way this works :(
>
>1) Yes it will use that controller. That route is hardcoded to use that controller since the controller is not a part of the route template.
>2) That route template will only work for your RunSheetDetailsAPI controller. You don't have any routes that map to your RunSheetDetails MVC controller. For the route for your MVC controllers, you will probably want to remove the "api" portion of the route, and you will want to add an action. So you will probably want to add:
config.Routes.MapHttpRoute(
>                name: "DefaultMVC",
>                routeTemplate: "{controller}/{action}/{id}",
>                defaults: new { action = "Index", id = RouteParameter.Optional }
>Then, your RunSheetDetailsAPI urls would be http://servername/api/routsheetdetails/id, and your RunSheetDetails MVC urls would be http://servername/routsheetdetails and http://servername/routsheetdetails/edit/id for your index and edit pages respectively. Note that both the route template and url include "api" for your API controllers.
>
>Also, since you are using both Web API and MVC in the same site and with similar names/routes, you may want to try adding constraints to the routes to ensure that your API routes use API controllers. See http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-route-constraint-cs.

OK, it hadn't clicked that some of the routes were MVC and some Web API!

Thanks
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Previous
Reply
Map
View

Click here to load this message in the networking platform