views:

49

answers:

3

I am attempting to construct an asp.net mvc app which will use the urls like:

/Controller/[Number]/Action/Id

I have got it to always call my controller and pass it the Number and the Id fine... However I now want to return a different view depending on the Number I could have options like:

if([Number] == 1) { return View("ViewName");}
if([Number] == 2) { return View("ViewName2");}

however I instead was wondering if there was a way to change the core so that instead of searching at ~/Views/controller/action.aspx I could have my own method which did some checking on the Number then passed to the virtual file provider is a different path

Hope this makes sense!

A: 

You might want to look at decorating your controller method with Action Filter Attributes.

Then, you could do something special inside the Action Filter Attribute.

Or, you could pass Number to a Model object, then have the model Object return the right View path.

Either way, your instinct of trying to keep too much logic out of the Controller is sound, especially if [Number] is somehow a business concern and not a view concern.

CubanX
Hi What do you mean by Action Attributes? Number is going to be a db lookup... I am going to load the view from the db depending on the Action and the [Number]
Mark Milford
Sorry, it's Action Filter Attributes. Read about them here: http://www.asp.net/mvc/tutorials/understanding-action-filters-csYou have full access to Context inside an Action Filter Attribute method.You should be able to do just about anything there. I have in the past done custom security at an object level there. Although, it might be overkill for your situation, it's something worth looking at.
CubanX
A: 

Decide which view to load, depending on input parameters is a controller task. You could write your own view engine.

But it is easier to return the full path to the view you want to return.

return View("~/myviews/ViewName3.aspx");

This will render ViewName3 from given directory.

Christian13467
A: 

You need to look into / google creating a custom view engine.

By the sounds of things you probably just want to extend the built-in WebFormViewEngine and just override the locations and the .FindView() method.

HTHs,
Charles

Charlino
HiTried that but issue is that I'm not just changing the search path but loading the view from the database... how do I create an IView from a string...
Mark Milford
Huh?? You didn't mention anything about loading the view from the database... in fact, you said "I could have my own method which did some checking on the Number then passed to the virtual file provider is a different path" but now you're changing your tune and you want to get it from the database?
Charlino