Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Web API MVC Controller - not finding correct controller
Message
De
01/07/2014 08:00:19
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Web API MVC Controller - not finding correct controller
Versions des environnements
Environment:
C# 4.0
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01602962
Message ID:
01602962
Vues:
46
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?
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform