开发者

'Global' state and ASP.NET MVC

I am playing with learning ASP.NET MVC as a non-web developer. I am trying to find the best idiom to use for an app that has开发者_如何学Python a concept of selecting a 'project' to work on the first page that affects all other pages.

There seems to be three choices:

  • Just put the information into the session state. Works fine, but isn't very MVC-ish
  • Embed the state into all URLs ... so instead of /Products/Details/1 the URLs are all /(project_id)/Products/Details/1
  • Setting a separate cookie for this information

Since nearly all the URLs in the application would require the current project this seems overkill, and makes constructing the URLs used in any of the views that much more work. It would also require that I validate the permissions on each call since the user could easily modify it.

Any suggestions on the best approach -- is using the session such a bad idea?!


Option 2 out of yours is my choice.
So instead of /Products/1/Details
I would make it Project/1/Products/1/Details

Its just more in line with REST. Its virtually irrelevant to MVC, but if you want your Routes and URLs to read like resources in REST, you'll want the url to collapse at the slashes and carry the state. Other ways are to mark a project id in cookie, but that kills linking, so that as someone leaves and comes back they get put back there.

Session also makes it hard to test if you bind yourself directly to that concept.


Personally, I would just use the session. There's nothing non-MVCish about the session really - think of it as just being part of your model.

Using a cookie is really no different than using the session. Embedding it in the URLs is not a bad option though, especially for "linkability" if that's a concern for your project. But it has the side effect of cluttering up URLs and requiring you to pass that ID around constantly from page to page.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜