有可能是ASP。NET MVC网站(不是项目)返回HttpResponseMessage

时间:2023-01-17 09:17:06

I am creating a RESTful webservice using ASP.NET MVC (not ASP.NET Web API). What I want to do is have every method in the controller return their result based on an input parameter (i.e. json or xml).

我正在使用ASP创建一个RESTful web服务。不净MVC(ASP。净Web API)。我要做的是让控制器中的每个方法基于输入参数(即json或xml)返回它们的结果。

If I were using ASP.NET Web API, the HttpResponseMessage works for this purpose. When I attempt to return an HttpResponseMessage from a controller in ASP.NET MVC, there is no detail.

如果我使用ASP。NET Web API, HttpResponseMessage就是为此目的而工作的。当我试图从ASP中的控制器返回HttpResponseMessage时。NET MVC,没有细节。

I have read that in this approach, I am supposed to use ActionResult. If I do this, then I need to create an XmlResult that inherits from ActionResult since it is not supported.

我读过这个方法,我应该使用ActionResult。如果我这样做,那么我需要创建一个XmlResult,它继承自ActionResult,因为它不受支持。

My question is why HttpResponseMessage does not work the same in both situations. I understand that in Web API, we inherit from ApiController and in ASP.NET MVC we inherit from System.Web.Mvc.Controller.

我的问题是为什么HttpResponseMessage在两种情况下都不一样。我理解在Web API中,我们从ApiController和ASP中继承。我们从System.Web.Mvc.Controller继承了NET MVC。

Any help is greatly appreciated.

非常感谢您的帮助。

Thanks,

谢谢,

EDIT 1 Much thanks to Fals for his input. My problem was in how to create an empty website and add all of the necessary functionality in. The solution was to use Nuget to get the packages mentioned in the comments and then to follow the steps in How to integrate asp.net mvc to Web Site Project.

编辑1非常感谢Fals的输入。我的问题是如何创建一个空的网站,并添加所有必要的功能。解决方案是使用Nuget获得评论中提到的包,然后按照如何将asp.net mvc集成到Web站点项目中的步骤进行。

2 个解决方案

#1


2  

Web Api is a Framework to develop Restfull services, based on HTTP. This framework was separeted into another assembly System.Web.Http, so you can host it everywhere, not only in IIS. Web API works directly with HTTP Request / Response, then every controller inherit from IHttpController.

Web Api是基于HTTP开发Restfull服务的框架。这个框架被分离到另一个assembly System.Web中。Http,因此您可以在任何地方托管它,而不仅仅是在IIS中。Web API直接使用HTTP请求/响应,然后每个控制器从IHttpController继承。

MVC has It's implementation on System.Web.Mvc. coupled with the ASP.NET Framework, then you must use It inside an Web Application. Every MVC controller inherits from IController that makes an abstraction layer between you and the real HttpRequest.

MVC在System.Web.Mvc上有它的实现。再加上ASP。NET框架,然后您必须在Web应用程序中使用它。每个MVC控制器都继承自IController,它在您和真正的HttpRequest之间创建一个抽象层。

You can still access the request using HttpContext.Response directly in your MVC controller, or as you said, inheriting a new ActionResult to do the job, for example:

您仍然可以使用HttpContext访问请求。直接在MVC控制器中响应,或者如您所说,继承一个新的ActionResult来完成这个工作,例如:

public class NotFoundActionResult : ActionResult
{
 private string _viewName;
 public NotFoundActionResult()
 {

 }
 public NotFoundActionResult(string viewName)
 {
  _viewName = viewName;
 }
 public override void ExecuteResult(ControllerContext context)
 {
  context.HttpContext.Response.StatusCode = 404;
  context.HttpContext.Response.TrySkipIisCustomErrors = true;

  new ViewResult { ViewName = string.IsNullOrEmpty(_viewName) ? "Error" : _viewName}.ExecuteResult(context);
 }
}

This ActionResult has the meaning of respond thought HTTP Error.

这个ActionResult具有response thought HTTP Error的含义。

#2


0  

As a matter of fact, it is indeed possible. You basically have two options:

事实上,这是可能的。你基本上有两个选择:

  • develop your custom ActionResult types, which can be an heavy-lifting work and also quite hard to mantain.
  • 开发您的自定义ActionResult类型,这可能是一项繁重的工作,而且很难维护。
  • add WebAPI support to your website.
  • 向你的网站添加WebAPI支持。

I suggest you to do the latter, so you will have the best of two worlds. To do that, you should do the following:

我建议你做后一种,这样你就会在两个世界中得到最好的结果。要做到这一点,你应该做到以下几点:

  • Install the following Web API packages using NuGet: Microsoft.AspNet.WebApi.Core and Microsoft.AspNet.WebApi.WebHost.
  • 使用NuGet安装以下Web API包:Microsoft.AspNet.WebApi。核心和Microsoft.AspNet.WebApi.WebHost。
  • Add one or more ApiControllers to your /Controllers/ folder.
  • 向/ controller /文件夹添加一个或多个ApiControllers。
  • Add a WebApiConfig.cs file to your /App_Config/ folder where you can define your Web API routing scheme and also register that class within Global.asax.cs (or Startup.cs) file.
  • 添加一个WebApiConfig。cs文件到您的/App_Config/文件夹,您可以在其中定义Web API路由方案,并在Global.asax中注册该类。cs(或Startup.cs)文件。

The whole procedure is fully explained here: the various steps, together with their pros-cons and various alternatives you can take depending on your specific scenario, are documented in this post.

整个过程在这里得到了充分的解释:不同的步骤,以及它们的优点,以及根据您的特定场景可以采取的各种选择,都在本文中有详细的说明。

#1


2  

Web Api is a Framework to develop Restfull services, based on HTTP. This framework was separeted into another assembly System.Web.Http, so you can host it everywhere, not only in IIS. Web API works directly with HTTP Request / Response, then every controller inherit from IHttpController.

Web Api是基于HTTP开发Restfull服务的框架。这个框架被分离到另一个assembly System.Web中。Http,因此您可以在任何地方托管它,而不仅仅是在IIS中。Web API直接使用HTTP请求/响应,然后每个控制器从IHttpController继承。

MVC has It's implementation on System.Web.Mvc. coupled with the ASP.NET Framework, then you must use It inside an Web Application. Every MVC controller inherits from IController that makes an abstraction layer between you and the real HttpRequest.

MVC在System.Web.Mvc上有它的实现。再加上ASP。NET框架,然后您必须在Web应用程序中使用它。每个MVC控制器都继承自IController,它在您和真正的HttpRequest之间创建一个抽象层。

You can still access the request using HttpContext.Response directly in your MVC controller, or as you said, inheriting a new ActionResult to do the job, for example:

您仍然可以使用HttpContext访问请求。直接在MVC控制器中响应,或者如您所说,继承一个新的ActionResult来完成这个工作,例如:

public class NotFoundActionResult : ActionResult
{
 private string _viewName;
 public NotFoundActionResult()
 {

 }
 public NotFoundActionResult(string viewName)
 {
  _viewName = viewName;
 }
 public override void ExecuteResult(ControllerContext context)
 {
  context.HttpContext.Response.StatusCode = 404;
  context.HttpContext.Response.TrySkipIisCustomErrors = true;

  new ViewResult { ViewName = string.IsNullOrEmpty(_viewName) ? "Error" : _viewName}.ExecuteResult(context);
 }
}

This ActionResult has the meaning of respond thought HTTP Error.

这个ActionResult具有response thought HTTP Error的含义。

#2


0  

As a matter of fact, it is indeed possible. You basically have two options:

事实上,这是可能的。你基本上有两个选择:

  • develop your custom ActionResult types, which can be an heavy-lifting work and also quite hard to mantain.
  • 开发您的自定义ActionResult类型,这可能是一项繁重的工作,而且很难维护。
  • add WebAPI support to your website.
  • 向你的网站添加WebAPI支持。

I suggest you to do the latter, so you will have the best of two worlds. To do that, you should do the following:

我建议你做后一种,这样你就会在两个世界中得到最好的结果。要做到这一点,你应该做到以下几点:

  • Install the following Web API packages using NuGet: Microsoft.AspNet.WebApi.Core and Microsoft.AspNet.WebApi.WebHost.
  • 使用NuGet安装以下Web API包:Microsoft.AspNet.WebApi。核心和Microsoft.AspNet.WebApi.WebHost。
  • Add one or more ApiControllers to your /Controllers/ folder.
  • 向/ controller /文件夹添加一个或多个ApiControllers。
  • Add a WebApiConfig.cs file to your /App_Config/ folder where you can define your Web API routing scheme and also register that class within Global.asax.cs (or Startup.cs) file.
  • 添加一个WebApiConfig。cs文件到您的/App_Config/文件夹,您可以在其中定义Web API路由方案,并在Global.asax中注册该类。cs(或Startup.cs)文件。

The whole procedure is fully explained here: the various steps, together with their pros-cons and various alternatives you can take depending on your specific scenario, are documented in this post.

整个过程在这里得到了充分的解释:不同的步骤,以及它们的优点,以及根据您的特定场景可以采取的各种选择,都在本文中有详细的说明。