繁体   English   中英

稍后添加了webAPI2的Web API 2和路由在MVC 5 Web应用程序中不起作用

[英]Web API 2 and routing not working in MVC 5 Web application with webAPI2 added later

我已经尝试了很多较新的问题和答案,但都找不到与Web API或操作资源有关的404错误,但没有运气。
最有前途的文章是这篇相当新近的文章。 但我仍然无法连接到控制器/动作。

我需要连接到的API项目是MVC 5 asp.net网络应用程序,其中包含所有最新版本的软件包。
我在其中添加了IdentityServer3,但有些困难(与其他软件包冲突,但仍然可以使用)。

该解决方案包含多个项目。
一个是新的空白项目,我在其中添加了Owin和startup.cs作为API。
我可以连接到这个。
另一个项目处理oidc-client进行登录和OpenId认证。
它有通过jQuery ajax调用API的np问题。

但是,我需要调用主要项目的API,但这会失败(正是在其中安装了IdentityServer3的该项目。)我已经在ajax函数中进行了尝试。
网址:“ http:// localhost:44888 / Values
网址:“ http:// localhost:44888 / api / Values

但都不起作用。

这是WebApiConfig类中的默认API配置路由表。
即routeTemplate:“ api / {controller} / {id}”

在项目上添加的新小项目没有默认的路由配置。

配置了webAPI rdefault路由的MVC项目的代码,我无法调用。

using System.Web.Http;  

namespace Catalyst.MainProject
{
/// <summary>
/// Best Practice: Enforces a “separation of concerns” between MVC and Web API, see WebApiConfig in App_Start folder.
/// see: https://www.ryadel.com/en/add-asp-net-web-api-support-to-an-existing-asp-net-mvc-web-application/
/// </summary>
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

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

        // WebAPI when dealing with JSON & JavaScript!
        // Setup json serialization to serialize classes to camel (std. Json format)
        var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
        formatter.SerializerSettings.ContractResolver =
            new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
    }
}
}

apiControllers

using System;
using System.Collections.Generic;
using System.Web.Http;

namespace Catalyst.Mvc
{
[Route("values")]
public class ValuesController : ApiController
{
    private static readonly Random _random = new Random();

    public IEnumerable<string> Get(string id)
    {
        var random = new Random();
        return new[]
        {
            _random.Next(0, 10).ToString(),
            _random.Next(0, 10).ToString(),
        };
    }
}
}

而我的ajax函数:

$.ajax({
    url: 'http://localhost:59754/Values',      //Note: Works,...this is the url for the Blank asp.net non MVC ApiProject
  //url: 'http://localhost:44888/Values',      //Note: Does not work, ... this is the url for the Api in Catalyst.Mvc .. the MVC non blank app

        method: 'GET',
        dataType: 'json',
        headers: headers
        }).then(function (data)
        {
            //LogAuthorizedUserInToMVCApp(data);
        }).catch(function (error)
        {
            display('.js-api-result', {
            status: error.status,
            statusText: error.statusText,
            response: error.responseJSON
            });
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM