我的Web api中有这些控制器方法,它们都接受HttpPost。 Process()方法在主体中接收复杂类型参数。 Install方法在主体中接收一个字符串参数。 成功调用了Process方法,但是Install方法失败,并显示错误404-未找到,我认为路由失败,但是我无法弄清楚我在做什么错...
[HttpPost]
[ResponseType(typeof(IProcessableObject))]
[Route("Workflow/Process")]
public IHttpActionResult Process([FromBody]SerializedObject request)
{
try
{
Type objectType = ResolveType(request.ObjectType);
IProcessableObject obj = (IProcessableObject)JsonSerializer.Deserialize(request.RawObject, objectType);
log.DebugFormat("Processing {0} with workflow {1}", objectType.Name, obj.WorkflowId);
var workflow = workflowController.Get(obj.WorkflowId, true);
var workflowProcessor = new WorkflowProcessor(obj, workflow);
if (workflowProcessor.Process())
return Ok(obj);
return InternalServerError();
}
catch (Exception ex)
{
log.Error(string.Format("Failed processing object {0}", request.ObjectType), ex);
return InternalServerError();
}
}
[HttpPost]
[ResponseType(typeof(int))]
[Route("Workflow/Install/{userName}")]
public IHttpActionResult Install(string userName, [FromBody]string xmlTemplate)
{
try
{
log.DebugFormat("User {0} is installing new workflow:{1}{2}", userName, Environment.NewLine, xmlTemplate);
var wf = workflowController.Install(xmlTemplate, userName);
if (wf == null)
return BadRequest();
return Ok(wf.WorkflowId);
}
catch (Exception ex)
{
log.Error("Failed installing workflow", ex);
return InternalServerError();
}
}
在我的MVC应用程序中,我这样称呼他们:
public static IProcessableObject Process(IProcessableObject obj, bool isProxy = false)
{
string requestURL = string.Concat(wfServiceUrl, "Workflow/Process");
var requestData = new SerializedObject
{
RawObject = JsonSerializer.Serialize(obj),
ObjectType = isProxy ? obj.GetType().BaseType.AssemblyQualifiedName : obj.GetType().AssemblyQualifiedName
};
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
var result = client.UploadString(requestURL, JsonSerializer.Serialize(requestData));
return (IProcessableObject)JsonSerializer.Deserialize(result, isProxy ? obj.GetType().BaseType : obj.GetType());
}
}
public static int Install(string workflowTemplate, string userName)
{
string requestURL = string.Concat(wfServiceUrl, "Workflow/Install/", userName);
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
var result = client.UploadString(requestURL, JsonSerializer.Serialize(workflowTemplate));
return JsonSerializer.Deserialize<int>(result);
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
}
答案 0 :(得分:0)
尝试更改此内容:
[Route(“ Workflow / Install / {userName}”)]
为此:
[Route(“ api / Workflow / Install / {userName}”)]
并使用其他路由执行相同操作,添加 api / ,这应该可行。