Mahdi Taghizadeh
Mahdi Taghizadeh

Reputation: 903

Is it necessary to have a separate View for each controller action?

Is there a mandatory relationship between a Controller Action and a View? I mean is it necessary to have a physical View (.aspx page) for each Action inside a Controller class?

Upvotes: 1

Views: 358

Answers (2)

Dan Atkinson
Dan Atkinson

Reputation: 11679

You can also return things like ContentResult in an action:

public ContentResult Index()
{
    return Content("Foobar!");
}

If this was called directly, this would be similar to:

Response.Write("Foobar!");
Response.End();

Upvotes: 2

Andrew Rimmer
Andrew Rimmer

Reputation: 3922

There is no mandatory relationship between the Controller Action and a view. The controller is responsible for returning an ActionResult. The most usual way of doing this is by using a View, but they aren't hard wired. A view could be shared across Controllers for instance.

Also a Controller, can deal with the request purely on its own, returning a redirect, or a JSON result, or even its own html (though not recommended).

Upvotes: 3

Related Questions