patterncsharpMinor
Is it ok to use HttpContext inside View?
Viewed 0 times
useviewhttpcontextinside
Problem
I have a pretty common issue: I need to know the name of current user and also name of the current controller so I can highlight/disable some links.
Here's some solutions I found by myself:
-
Get all what's needed inside
I have a PartialView inside my page layout, that takes user name like this:
And here's the way I take controller name:
I have some problem with this code, for it is suspected to ruin MVC pattern. I have another solution, which I consider even more stupid.
-
I've created new controller called like
Here's the problem: I cannot get controller name with this way, because it will always be like "Context", which is useless for me. And I also get the name on view like this:
-
The last way I figured out is to pass needed strings through
But here comes another problem: I have whole lot of AJAX in my project, and it seems to me not legit enough to pass unused data every time controller action is called.
If you know of a better solution, I'd be happy to hear it.
Here's some solutions I found by myself:
-
Get all what's needed inside
PartialView.I have a PartialView inside my page layout, that takes user name like this:
@{ var userName = Context.ToContext().GetUser().Name; }And here's the way I take controller name:
@{ var controllerName = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString(); }I have some problem with this code, for it is suspected to ruin MVC pattern. I have another solution, which I consider even more stupid.
-
I've created new controller called like
ContextController:public class ContextController : BaseController // BaseController is just a facade
{
public string GetUserName() {
return Context.GetUser().Name;
}
}Here's the problem: I cannot get controller name with this way, because it will always be like "Context", which is useless for me. And I also get the name on view like this:
@{ var userName = Html.Action("GetUserName", "Context"); }-
The last way I figured out is to pass needed strings through
ViewData. But I have about 18 controllers and some of 'em have like 3 methods, returning ViewResult. It's not bad design, it's just reality. And I don't actually want to create and pass ViewDataDictionary for every single method, that returns ViewResult. Maybe I could extend my BaseController, so ViewDataDictionary for every page will always have userName and controllerName?But here comes another problem: I have whole lot of AJAX in my project, and it seems to me not legit enough to pass unused data every time controller action is called.
If you know of a better solution, I'd be happy to hear it.
Solution
You should try creating ViewModel classes - they're simple classes which just have a bunch of properties, and you pass them to the view instead of using
This will make your markup more readable, and ensure type-safety of your data, which will make working with it easier.
Here's an example, although you'll have to convert it from ASPX to Razor.
ViewData or ViewBag. You can make them all inherit off of a base model which has a bunch of show/hide flags (like user1614543 suggested) or put the flags on individual models (the better choice) which get returned to the views that use those flags. Either way, you can put in sane default values so you only need to set them when you're displaying different data. You can even pass the name of the controller as a string, or use an enum to categorize them.This will make your markup more readable, and ensure type-safety of your data, which will make working with it easier.
Here's an example, although you'll have to convert it from ASPX to Razor.
Context
StackExchange Code Review Q#18606, answer score: 5
Revisions (0)
No revisions yet.