English 中文(简体)
如何接收 Http 请求进入 MVC 3 控制器操作?
原标题:How to receive a HttpRequest into an MVC 3 controller action?

Current Solution

所以,我有非常相似的东西

[HttpPost]
    public ActionResult Upload()
    {
        var length = Request.ContentLength;
        var bytes = new byte[length];

        if (Request.Files != null )
        {
            if (Request.Files.Count > 0)
            {
                var successJson1 = new {success = true};
                return Json(successJson1, "text/html");
            }
        }
...
        return Json(successJson2,"text/html");
    }

Unit testable solution?

我想要这样的东西:

[HttpPost]
public ActionResult Upload(HttpRequestBase request)
{
    var length = request.ContentLength;
    var bytes = new byte[length];

    if (request.Files != null )
    {
        if (request.Files.Count > 0)
        {
            var successJson1 = new {success = true};
            return Json(successJson1);
        }
    }

    return Json(failJson1);
}

然而,这失败了, 令人烦恼,因为我可以做一个模克 从基级,并使用它。

Notes

  • I am aware this is not a good way to parse a form/upload and would like to say other things are going on here (namely that this upload can be a form or an xmlhttprequest - the action does not know which).
  • Other ways to make "Request" unit testable would also be awesome.
最佳回答

您已经有一个 < a href=> http://msdn.microsoft.com/ en- us/library/ system.web.mvc. controller. request. aspx" request. rel= "noreferr" > request

[HttpPost]
public ActionResult Upload()
{
    var length = Request.ContentLength;
    var bytes = new byte[length];

    if (Request.Files != null)
    {
        if (Request.Files.Count > 0)
        {
            var successJson1 = new { success = true };
            return Json(successJson1);
        }
    }

    return Json(failJson1);
}

您可以在单位测试中模拟 request , 更具体地说, HttpContext 具有请求属性:

// arrange
var sut = new SomeController();
HttpContextBase httpContextMock = ... mock the HttpContext and more specifically the Request property which is used in the controller action
ControllerContext controllerContext = new ControllerContext(httpContextMock, new RouteData(), sut);
sut.ControllerContext = controllerContext;

// act
var actual = sut.Upload();

// assert
... assert that actual is JsonResult and that it contains the expected Data
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签