Where should redirects be done

2019-09-07 06:55发布

I have searched a little around the internet looking for what might be a simple question. When using the MVC style of programming, where should a HTTP redirect be performed? Some say its a job for the controller layer and others say it is a job for the view layer.

From my current understanding of MVC it would be the controller as it coordinates the business logic. But I understand those who claim it would be the view layer with the argument that a redirect changes the visual presentation a client would receive.

Can anyone shed some light on this?

3条回答
太酷不给撩
2楼-- · 2019-09-07 07:11

Sorry for my english. I mean View-layer could be used in wide and in narrow senses.

Wikipedia and some other sources describe View layer like a logic for every response. It belongs to the set of all available server responses.

But in practice there is different approach. All system statuses like 300, 400, 401, 403, 404 are managed from controller or router. It makes sense because a designer or an html-coder could make his job without knowledge how server-side technologies work.

查看更多
该账号已被封号
3楼-- · 2019-09-07 07:14

Views in MVC structure is responsible for producing the response to user's activities. HTTP Location header would be part of that response.

Also, controllers are not responsible for dealing with business logic. The business logic is contained in the model layer. The controllers instead are in charge of altering the state of mode layer.

查看更多
不美不萌又怎样
4楼-- · 2019-09-07 07:22

You are right. Redirecting is part of the application logic, which is done in the controller. That's why you have a RedirectToAction, for example. Also read here.

A concrete example is the POST-Redirect-GET pattern, where you initiate a redirect at the end of a POST action method in a controller.

A view is responsible for view-related tasks, like rendering the UI based on a dedicated view model; and dealing with UI interactivity; but should not be aware of application logic.

查看更多
登录 后发表回答