Asp.net mvc 4 Asp.net MVC 4路由失败

Asp.net mvc 4 Asp.net MVC 4路由失败,asp.net-mvc-4,routes,restsharp,azure-compute-emulator,Asp.net Mvc 4,Routes,Restsharp,Azure Compute Emulator,我对MVC比较陌生,但已经成功地用它构建了一个标准的用户驱动的CRUD站点,所以我有一些基本知识。我开始开发一个用MVC4构建的REST-ful api应用程序,我确信这是一个新手问题,需要在这个新应用程序中向控制器发出一个简单的请求 在一个旨在支持POST操作的简单测试资源上,我得到的是404而不是202。我在这个网站上搜索了路由问题的解决方案,尝试了许多不同的硬连线和参数化路由以及默认值的组合,但还没有找到任何我认为很容易解决的问题 更新 我还尝试将路由配置设置(请参见下面的路由配置1和2)

我对MVC比较陌生,但已经成功地用它构建了一个标准的用户驱动的CRUD站点,所以我有一些基本知识。我开始开发一个用MVC4构建的REST-ful api应用程序,我确信这是一个新手问题,需要在这个新应用程序中向控制器发出一个简单的请求

在一个旨在支持POST操作的简单测试资源上,我得到的是404而不是202。我在这个网站上搜索了路由问题的解决方案,尝试了许多不同的硬连线和参数化路由以及默认值的组合,但还没有找到任何我认为很容易解决的问题

更新 我还尝试将路由配置设置(请参见下面的路由配置1和2)更改为使用MapHttpRoute()而不是MapRoute()。这没有任何效果

这两个web应用都托管在一个Azure web角色中,配置为两个不同的站点,在两个不同的端口上侦听。我正在排除的问题是在我的本地计算机上,因此使用compute emulator时,所有URL都以以下开头:

http://127.0.0.1:<port>/
…以下是用于序列化数据并转换为字符串的代码。我知道RestSharp将为我进行序列化,但我有其他特定于应用程序的原因,我自己进行序列化,我认为这与问题无关。如果这与问题有关,我们可以去那里,但目前它不在我的嫌疑犯名单上:

序列化方法
私有XML文档序列化(T数据)
{
XmlSerializer ser=新的XmlSerializer(theData.GetType());
XmlDocument xml=新的XmlDocument();
使用(MemoryStream stream=new MemoryStream())
{
序列化(流、数据);
stream.Flush();
stream.Seek(0,SeekOrigin.Begin);
加载(流);
}
返回xml;
}
私有字符串GetStringFromXmlDocument(XmlDocument theDoc)
{
字符串结果=null;
使用(var stringWriter=new stringWriter())
使用(var xmlTextWriter=XmlWriter.Create(stringWriter))
{
Doc.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
结果=stringWriter.GetStringBuilder().ToString();
}
返回结果;
}
…下面是使用数据类和上述方法的RestSharp客户端代码:

夏普客户
XmlDocument theSerializedData=序列化(新测试数据
{
日期=日期时间。现在,
IsTrue=false,
Name=“奥斯卡”
} );
string theDataString=GetStringFromXmlDocument(序列化数据);
RestClient=新的RestClient(“http://127.0.0.1:7080/rest/testing");
重新请求=新的重新请求(“测试”,Method.POST);
AddParameter(“text/xml”,测试数据,ParameterType.RequestBody);
IRestResponse response=client.Execute(请求);
if(response.StatusCode==HttpStatusCode.OK)
{
;//做一张快乐的脸
}
其他的
{
//
}
…以下是我尝试过的两种路由配置:

路由配置#1
publicstaticvoidregisterOutes(路由收集路由)
{
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
routes.MapRoute(
名称:“restest”,
url:“rest/{controller}/{action}”,
默认值:新建{controller=“Home”,action=“Index”,id=UrlParameter.Optional}
);
routes.MapRoute(
名称:“默认”,
url:“{controller}/{action}/{id}”,
默认值:新建{controller=“Home”,action=“Index”,id=UrlParameter.Optional}
);
}
路线配置#2
公共静态无效寄存器(HttpConfiguration配置)
{
config.Routes.MapHttpRoute(
名称:“restest”,
routeTemplate:“rest/{controller}/{action}”,
默认值:新建{controller=“Testing”,action=“Tests”}
);
}
…这是控制器:

控制器
公共类测试控制器:ApiController
{
[HttpPost]
公共无效测试(测试数据)
{
bool isTru=数据IsTrue;
}
}
…以下是从Fiddler捕获的原始请求(主机名替换为127.0.0.1):

Http请求
您需要使用“路由”。MapHttpRoute(“用于注册Api路由和目标APIController的扩展。当前您使用的是用于MVC控制器的MapRoute)。

您需要使用“路由”。MapHttpRoute(“用于注册Api路由并以APIController为目标的扩展。当前您正在使用用于MVC控制器的MapRoute。

此问题专门用于在Azure compute emulator上以web角色运行rest应用程序。当我在VS中右键单击应用程序并选择“在浏览器中查看”或将其设置为默认项目时ect并运行Debug->Start new Instance,我可以从浏览器中调用rest API并获得响应。当我在Azure compute emulator上以web角色启动应用程序时,我会得到404,就好像所有注册的路由都没有实际注册一样


我可能会发布另一个与compute emulator和WebAPI应用程序相关的不同问题,但目前我的代码似乎没有问题。可能是我的Azure dev设置存在配置或其他系统/环境问题,或者compute emulator中存在一些缺陷。

此问题与在Azure compute emulator上以web角色运行rest应用程序。只要在VS中右键单击该应用程序并选择“在浏览器中查看”,或将其设置为默认项目并运行调试->启动新实例,我就可以调用rest
  POST http://127.0.0.1:7080/rest/testing/tests HTTP/1.1
  Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
  User-Agent: RestSharp 104.1.0.0
  Content-Type: text/xml
  Host: 127.0.0.1:7080
  Content-Length: 243
  Accept-Encoding: gzip, deflate

  <?xml version="1.0"?>
  <TestData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Name>Oscar</Name>
    <Date>2013-05-16T11:50:50.1270268-07:00</Date>
    <IsTrue>false</IsTrue>
  </TestData>
The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.
Please review the following URL and make sure that it is spelled correctly.

Requested URL: /rest/testing/tests

[HttpException]: The controller for path '/rest/testing/tests' was not found or does not implement IController.
at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)