Question

I have an existing ASP.Net MVC 5 project, with Fluent NHibernate and Web API. I include it in an Orchard Module in order to work with Orchard CMS, but the problem I got is in my view (Policy_Lookup.cshtml), the function I have does not retrun me the data and I dont know why. in the Google Ghrome Console it shows me this error (Failed to load resource: the server responded with a status of 404 (Not Found) "http:/localhost:30321/api/VinSearch/getVinResults?VinNum=JT4RN50R9E0009467&Agency=09207")

Here is my Policy_Lookup.cshtml file:

@using Orchard.UI.Resources;

@{
    Script.Require("jquery-1.10.2.js");
}
@using(Script.Head())
{

    <script type="text/javascript">
        // URI of the controller
        var URI = "../api/VinSearch";
        @*@Url.Action("Policy_Lookup","Home", new { area = "OIS_New"})*@
        function displayVinResults(VinNum, Agency)
        {
            console.log("hhhh");
            // Send an AJAX request to the controller VinSearch and action getVinResults
            //$.getJSON(URI + '/getVinResults/' + VinNum + '/' + Agency)
            $.getJSON("../api/VinSearch/getVinResults?VinNum=" + VinNum + "&Agency="+Agency)
                .done(function (data) {
                    // code to execute when the data is retrieved from the controller
                    console.log("gbbgb");
                    // check if the data is empty
                    if (data.length == 0) {
                        $("#error").show();
                        $("#btnVinSearch").show();

.........

Here is the route file: public class Routes : IRouteProvider { public void GetRoutes(ICollection routes) { foreach (RouteDescriptor routeDescriptor in GetRoutes()) routes.Add(routeDescriptor); }

    public IEnumerable<RouteDescriptor> GetRoutes()
    {
        return new[] {

            new RouteDescriptor {
                Priority = 5,
                Route = new Route(
                    "OIS_New",
                    new RouteValueDictionary {
                        {"area", "OIS_New"},
                        {"controller", "Home"},
                        {"action", "Policy_Lookup"}
                    },
                    new RouteValueDictionary(),
                    new RouteValueDictionary {
                        {"area", "OIS_New"}
                    },
                    new MvcRouteHandler())
            }
        };
    }

Here is my WebAPI:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace OIS_New
{
public class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
        config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
        config.Formatters.Remove(config.Formatters.XmlFormatter);//to set json response as a default display
        // Web API routes
        config.MapHttpAttributeRoutes();
        //to configure the routing convention
        config.Routes.MapHttpRoute(
            name: "VinSearch",
            routeTemplate: "api/{controller}/{action}/{VinNum}/{Agency}",
            defaults: new { VinNum = RouteParameter.Optional, Agency = RouteParameter.Optional }
        );
        config.EnsureInitialized();
    }
}
}

And this is the action in the controller file:

public List<VINResult> getVinResults(String VinNum, String Agency)
{
return dbh.GetVinToVinResult(VinNum, Agency);
}

Note: When I run the project, the compiler does not reach this function

Était-ce utile?

La solution

Routes in Orchard are declared in an implementation of IRouteProvider, even WebAPI routes. Your Register method is probably never hit. See http://sebastienros.com/web-api-makes-it-in-orchard for details about the WebAPI implementation in Orchard.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top