为什么创建的应用程序作为webapi工作,而在现有MVC项目中添加的相同webapi不会路由? [英] Why does an app created as a webapi work, while the same webapi added within an existing MVC project will not route?

查看:86
本文介绍了为什么创建的应用程序作为webapi工作,而在现有MVC项目中添加的相同webapi不会路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的Web API测试应用程序(作为Web API应用程序创建)从URL完美地运行,那么为什么复制到直接MVC应用程序中的完全相同的代码无法路由的原因可能是什么?如果我在创建MVC应用程序时没有选择WebAPI,是否有一些必要的缺失?我正在尝试将WebAPI(从SQL Server存储过程中检索数据)添加到现有的MVC应用程序。我去了模型浏览器,并确保并执行功能导入以引入字段,就像我在测试应用程序中所做的那样。



我没有为WebAPI使用单独的项目;它只在一个项目中,所以端口号没有问题。我已经被困在这几天了。我已经多次在MVC应用程序中删除并重新创建了Model。添加了Web API的测试应用程序和MVC应用程序具有完全相同的APIController和相同的WebApiConfig.cs文件。当我在浏览器中输入带有数据的URL时,它在测试应用程序中工作,给出对浏览器的正确响应,但不在MVC应用程序中。



使用MVC应用程序时对浏览器的响应是:



'/'中的服务器错误申请

无法找到资源。



如果有人知道可能的原因,请告诉我。谢谢。



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



WebApiConfig.cs



// Web API路由

config.MapHttpAttributeRoutes ();



config.Routes.MapHttpRoute(

name:DefaultApi,

routeTemplate: api / {controller} / {id},

默认值:new {id = RouteParameter.Optional}

);





控制器

公共类CompletedRoundsController:ApiController

{

// GET

public IEnumerable< string>获取(int ID,字符串AsmtType)

{

使用(WebAPIEntities db = new WebAPIEntities())

{

List< string> R = db.spCurrentRounds(ID,AsmtType).Select(x => x.Completed).ToList();

返回R;

}

}



我尝试过:



http :// localhost:58398 / api / CompletedRounds?ID = 1& AsmtType = ABC

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

推荐答案

我找到了解决方案。虽然WebAPI控制器脚手架自动添加了WebApiConfig.cs文件,但它没有确保Global.asax文件中存在以下两行,所以我添加了它们并且它现在工作得很好。





使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.Http;

使用System .Web.Mvc;

使用System.Web.Optimization;

使用System.Web.Routing;

使用System.Web.Security;

使用System.Web.SessionState;



名称空间CCOutcomesWeb

{

公共类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);

}

}

}
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);
}
}
}


这篇关于为什么创建的应用程序作为webapi工作,而在现有MVC项目中添加的相同webapi不会路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆