开发者

MVC3 = >> return EmptyResult() When it's a good idea to return this?

whe开发者_运维百科n it's a good idea to return "EmptyResult()" from a Controller


You would basically use it to signify that you are not doing anything with an action's result.

From MSDN:

Represents a result that does nothing, such as a controller action method that returns nothing.

I have personally used on actions defined in an AsyncController, so if you have for instance an async action like:

public void SendMailAsync() { }

Basically an action in an AsnycController, you'll need a xxxCompleted action as well (by convention)

public virtual ActionResult SendMailCompleted
{
    // do whatever
    return new EmptyResult();
}

Since this is not an action meant to be called by a user but by a background task, I'm not going to do anything with the result anyway.


I've used it when creating RESTful web services. When doing a POST or DELETE operation, for example, the HTTP status code can convey enough info in itself.


When the ajax performs an action that doesn't need reflection/confirmation on the UI.


I'm assuming its the same as doing return (null) in the action. It could be useful at times. I've used it to Response.Write info to the output while debugging, but didn't need the remainder of the view rendered.

When you use it, you'll get a blank white page with nothing on it, unless you output something of your own.


For a .NET Core solution (tested in v2.1), use:

return StatusCode(StatusCodes.Status204NoContent);

This will return a StatusCodeResult with the appropriate HTTP status code 204 No Content.


for example: You can return empty result with status 401.


The EmptyResult is a class in MVC which does not return anything, like Void method .

EmptyResult is used when you want to execute logic written inside the controller action method but does not want any result back to the view then EmptyResult return type is very important . It does not require to add the view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜