MvcCodeRouting: "Link" views to the same route as the controller - asp.net-mvc

I have a Controller structure that looks like this:
[Controllers]
[Users]
[PlayersController.cs]
[ServicesController.cs]
Where Players and Services are the two different types of users.
If i call the controller like this: localhost:xxxx/Users/Players/ index is being called, as expected, but my View is not rendered. My view is under ~/Views/Users/Players/Index.cshtml but ASP.NET is not looking for it under that folder.
I read that I should use EnableCodeRouting but I'm not sure where and how to use it:
https://github.com/maxtoroq/MvcCodeRouting/blob/master/docs/api/MvcCodeRouting/CodeRoutingExtensions/EnableCodeRouting_1.md
I added a that method under App_Start\RouteConfig.cs but it's never being called.
How do I, without typing in the View link in the controller, tell MvcCodeRouting where my View is located?
I.e. I want to use return View(vm) not return View("~/Views/Users/Players/index.cshtml").

In global.asax.cs.Application_Start() add this: RegisterViewEngines(ViewEngines.Engines);
And in the same class create this method:
void RegisterViewEngines(ViewEngineCollection viewEngines)
{
// Call AFTER you are done making changes to viewEngines
viewEngines.EnableCodeRouting();
}

Related

How do I call a method on one controller from another in .NET 4 MVC

In an asp.net MVC application, I need to produce some documens, HTML and PDF, which are not sent to the user's browser, but either sent by mail or entered in our document journalizing system. I produce these documents using Razor.
When a document is used only once, I just add a method to the relevant controller, and the view to that controller's view folder. This works. But I have a document that must be produced at two places in the application, implemented in separate controllers. I have made a new controller for this document with its own view folder.
My question is now: how do I call a method on this controller? Searching the web gives many answers, but all redirect the user to this document, which is not what I need.
You can just call it like you would any other method e.g.
public ActionResult DoSomething()
{
// Some code
var otherController = new OtherController(); // The other controller where the method is
otherController.CreatePdf(); // Call the method
// Continue with what ever else you need to do
return View(); // This will then return the `DoSomething` View
}
But personally it doesn't seem like this logic belongs in a controller. You should possibly think about refactoring this logic out of a controller and into a more logical place. Possibly create your own document generation class and use that.
If I'm getting you right.You could create a base controller and add the method there. You can inherit the Base controller in any controller where you want to call the method. here's a link that might help show you the use of Base controllers. How to wire common code from a base controller in ASP.NET MVC.

MVC - How to include a model inside _Layout for Partial View

I've seen how to use Partial Views from within a View and I understand how a model is passed from the View to a Partial View. What I don't grasp is how to include a Partial View inside of _Layout.cshtml so that I can pass it to the Partial View - or, how the Partial View itself can call a Controller Action to create a Model for use.
Specifically, I want to include a Partial View within _Layout.cshtml to display in the header of every page. The Partial View will display a custom setting in the User Profile. I can't think of any way to obtain the Model without making a Controller Action call - but how is this done in/for a Partial View from within _Layout.cshtml?
Is my only option of accessing a Controller Action to build my Partial View's required Model to use a jQuery call? Or is there another way?
The answer is by calling #Html.Action().
It sounds like you're on the right track.
The key is to try to not pass multiple models around, make models complicated, or use the ViewBag needlessly.
Instead, any time you want information on every page, call an action from your _Layout.
For example, you might have a controller called PartialsController or SharedController.
public class PartialsController : Controller
{
[ChildActionOnly]
public ActionResult UserProfilePartial()
{
UserProfileModel model = new UserProfileModel();
return PartialView("_UserProfile", model);
}
}
The ChildActionOnlyAttribute means that users cannot access the action directly. Only your code can call the action. You can also apply the attribute to the controller so that it affects all actions automatically.
Now call the action from your view (_Layout).
#Html.Action("UserProfilePartial", "Partials")

Grails view URL Mapping

I'm kind of new to grails and I'm trying to just map a basic URL request to a view.
So, say I have a view, /x/index.gsp and I want the user to be able to go to it. There will also be /y/index.gsp, /z/index.gsp, etc.
I defined it like so:
"/$customer/index" { view = {params.customer+"/index"} }
This seems to throw an exception though. I also have :
"/$customer/$controller/$action?/$id?" { }
which does work and I don't want to have to create a controller that doesn't really do anything but handle the index call and show it.
I'm sure I'm missing something simple but I don't know what it is.
The reason the first mapping fails is because it can't figure out what controller to route the request to.
To fix it, you need to define what controller you want the top mapping to route to. This is how I did this in a recent project of mine:
"/uploaders/$id" {
controller: "uploader"
}
To map to just a view:
"/$customer/index"(view: "/${params.customer}/index")

Problem returning specific view in asp.net mvc3

I have a view file structure like:
Views
Company
Department
Employee
ManageEmployee.cshtml
and the controller is
public class EmployeeController : Controller
{
public ActionResult Index(int dptId)
{
var loadedEmp = getEmpOf(dptId);
return View("Company/Employee/ManageEmployee", loadedEmp);
}
}
But the controller give me an error - telling that it can't find the view.These are the paths it search.
~/Views/Employee/Company/Employee/ManageEmployees.aspx
~/Views/Employee/Company/Employee/ManageEmployees.ascx
~/Views/Shared/Company/Employee/ManageEmployees.aspx
~/Views/Shared/Company/Employee/ManageEmployee.ascx
~/Views/Employee/Company/Employee/ManageEmployee.cshtml
~/Views/Employee/Company/Employee/ManageEmployee.vbhtml
~/Views/Shared/Company/Employee/ManageEmployee.cshtml
~/Views/Shared/Company/Employee/ManageEmployee.vbhtml
Basically if I'm able to eliminate the Employee section, the engine will find it.
~/Views/Employee/Company/Employee/ManageEmployee.cshtml to this
~/Views/Company/Employee/ManageEmployee.cshtml
Any insights on how to achieve this.
Thanks.
Have you tried:
return View("/Company/Employee/ManageEmployee", loadedEmp);
It looks like the engine is trying to return the view relative to your current location in the site rather than from the root of the site.
View has to be returned from the controller in the following way (for Specific View):
return View("ManageEmployee", loadedEmp);
In MVC, the controller will automatically route to the View name you provided.
loadedEmp should be the object you are passing to the view.
You need to follow MVCs convention of ControllerNameController for your controller and your view structure of ControllerName/...
If you want full control over your structure you'll need to switch to a different framework like FubuMVC.
If you want your own convention of arranging the views folder structures, it would be better you plug in your own view engine.

asp.net mvc how it decides which view to load

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!
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.
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.
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

Resources