I write my code at ASP.NET Web API 4.5 and use nuget package Swashbuckle -https://github.com/domaindrivendev/Swashbuckle
I have a lot of controllers in my project , and I want that every controller have a special page with description and with the swagger operations
Until now:
I override the index.html page with CustomAsset function:
c.CustomAsset("index", typeof(SwaggerConfig).Assembly, "proj.index.html");
Set description in the index.html to controller actions
Write function of Filter apply and using the object SwaggerDocument swaggerDoc I delete all the controllers instead the one I want to show like this:
swaggerDoc.paths.Remove(path i dont want)
The question if I can do separate my controllers to different pages ( lets say every controller have his own index.html file)?
Thanks
Related
Basically I have a webpage structure where common parts of the page (header, side bar, etc) are generated in a separate controller filled with child actions outputting partial views. I then call these actions (using RenderAction()) from the layout page of the website.
So (if I'm right in saying this), There are multiple internal mvc pipelines (header/sidebar internal requests) including the original request pipeline to for the specific webpage. How/Where can I initialize some data from the original pipeline request and have that data accessible from the other internal mvc pipeline requests?
Summary of what I want to accomplish (with example)
Request for website comes in.
MVC starts pipeline for "Home" controller, "index" action.
Before the Action gets executed, some data needs to be created that can later be accessible.
From the layout page, several "RenderAction" Methods get executed creating sub pipelines for interal requests (e.g. "Shell" controller, "DisplayHeaderBar" action
"DisplayHeaderBar" needs to access some data that was set in step 3 before rendering partial view
Hopefully this makes sense...
What you're looking for are child actions. You simply create an action in some controller that returns a partial view. For example, you could handle your site navigation via:
[ChildActionOnly]
public ActionResult SiteNavigation()
{
// get the data for your nav
return PartialView("_SiteNavigation", yourSiteNavModel);
}
The ChildActionOnly attribute ensures that this action can only be called as a child action, making it inaccessible via typing a URL in a browser's navigation bar.
Then, you create a view in Views\Shared\_SiteNavigation.cshtml:
#model Namespace.To.ClassForSiteNavigation
<!-- render your site navigation using the model -->
Finally, in your layout:
#Html.Action("SiteNavigation", "ControllerWhereThisExists")
I think you could use Tempdata for that. Tempdate gets deleted after you access it, so if you want to use the data more then once use Tempdata.Peek or Tempdata.Keep.
Here is a link with some explanation how you can pass data in asp.net mvc.
https://msdn.microsoft.com/en-us/library/dd394711%28v=vs.100%29.aspx
If tempdata doesn't do it then you could use cache.
Just playing around with Sitecore 7 and MVC, and I try to get the rendering basics working.
So far, I have been able to create a View Rendering (and mapped to the relevant .cshtml file) within the Renderings section, and applied these to the presentation details of the item (in much the same way you do with ASPX Layouts/ASCX Sublayouts).
I have also been able to map the Item to a controller (using the Controller and Action fields on the item), have the Index action on the controller (inherited from SitecoreController) return the view ~/Views/Home/Index.
The issue I can't seem to wrap my head around is merging the two rendering methods. I want to be able to create controllers that map to an Item, but render the item using the ViewRenderer, rather than using the default MVC conventing of return View(), so that I can:
Specify the location of the view files within a multi-site environment by setting the path parameter of the rendering; and
Have content authors/managers manage the renderings the way that the Layout/Sublayout does with place holders.
Does anyone know of a way that this can be achieved?
Have you taken a look at Controller Renderings in Sitecore MVC? These give you the ability to map a controller class to a Sitecore presentation item that can be statically or dynamically bound to your layout details.
This post has a reasonable overview of how to get started with controller renderings.
As for specifying the location of View files for multi-site environments you can pass the path to the razor file into the Controller View method, for example:
return View("~/Areas/SampleArea/Views/SampleArea/Index.cshtml");
I hope this helps.
I want to create module based structure, Like in zend framework. Suppose I have 2 controllers like student and teacher and I want to put all these in one folder say shchool. Same way I want the view files for each controller in school folder for e.g
For Controller:
D:\aspprojects\Project1\Project1\Controllers\School\TeacherController.cs
D:\aspprojects\Project1\Project1\Controllers\School\StudentController.cs
and for view files:
D:\aspprojects\Project1\Project1\Views\School\Teacher\all CRUD files(*.cshtml)
D:\aspprojects\Project1\Project1\Views\School\Student\all CRUD files(*.cshtml)
Current structure is like as below,
for Controllers:
D:\aspprojects\Project1\Project1\Controllers\TeacherController.cs
D:\aspprojects\Project1\Project1\Controllers\StudentController.cs
For view files
D:\aspprojects\Project1\Project1\Views\Teacher\all CRUD files(*.cshtml)
D:\aspprojects\Project1\Project1\Views\Student\all CRUD files(*.cshtml)
What changes do I need to do?
The problem you are facing is that MVC doesn't care what folder the controller is in. In fact, it doesn't even have to be in a folder called Controllers. MVC only looks for classnames with Controller in the name. Once compiled, the folder structure is lost, and as such, no way for the framework to know to look in a subfolder for a view, because that information is no longer present in the compiled code.
You can still do it, however.. but you can no longer rely on MVC to find your view files automatically, you will have to pass each view name directly.
This means you will have to do this:
return View("~/Views/School/Teacher/Index.cshtml");
Rather than this.
return View();
Another option is to use areas, which allows you to create a School area, and then you can create a teacher and student controllers within the school area.
Is there a recommended approach for creating Google Sitemaps using ASP.NET MVC?
I'm new to MVC and this is the first time I've needed to create one and wondered how best to go about it.
I have a number of static links (About Us, FAQ's etc.) that I would like included within the sitemap, but then need the rest of it to be dynamically generated from articles that have been posted on the site.
Any advice/direction on how to create this would be much appreciated.
1 - The first thing you'll need is to create a representation of your entire website a list of nodes which have children, parents and so forth. The easiest way to do this without rolling your own solution is to use the MVCSiteMapProject. It allows you so use MVCish terms like your action and controller names to define nodes which will automatically have the correct urls using your routing definitions.
2 - Now because the MVCSiteMap inherits from the default XmlSiteMap (may not have the exact name right ) you can use another add in to generate a google sitemap from the nodes you've defined in the MVCSiteMapProject.
There a bunch of ways to do #2 so its up to you to decide the technique.
You could create a generic handler the same as web forms, but I'd be inclined to use a controller action and a custom route.
Some simple steps to follow might be:
Create an action in your Home Controller (or create a new one), call it SiteMap.
Have the action return a View with your page data as the model.
Create a View called SiteMap that contains the Google XML, then iterate through your page data to generate the dynamic content.
Add a custom route to your Global.asax file that points to "/sitemap.xml" or whatever and pre-populate your the controller and action parameters with that of your new action.
If you're unsure of custom routes, just copy the default one and paste it above. The routes are handled first come first serve. Make sure you give it a new name.
Rich
for dynamic sitemap, i found this is the best solution : http://ben.onfabrik.com/posts/generating-dynamic-xml-sitemaps-in-aspnet-mvc
it use a controller to generate the xml file.
We are beginning the process of moving from Web Forms to MVC for all of our new applications. I am working on porting our Master Page over and am trying to satisfy the requirements that we need a single master page to be used by all applications. The primary navigation for the application needs to be in a menu within the master page. Accomplishing this was easy, the hard part is that each application may need to determine what to display in the menu using a unique set of rules. Some apps can simply say, here's the menu structure to use via something like a SiteMap. Others need to determine what is displayed in the menu based on what roles the user has, this can also be handled easily with a SiteMap. The situation that I'm struggling with is that some apps need to generate the menus based on the roles the user has, but also on the data on which they are working. i.e. The same user may have different option in the menu for a page if they are working on object 'foo' than they do if working on object 'bar'.
What I've done at this point, is I've created an HtmlHelper that is called by the master page view and takes a list of objects of a custom type and returns an unordered list that is styled by a jQuery plugin to display the menu. The list of objects the helper method takes are passed to the view using the ViewData dictionary. Currently, the value of this ViewData node is set within the constructor of each controller. This allows each page, and potentially each method, to set a different menu without having to set the value in each action method, unless its needed. I have also created a class that parses a SiteMap and returns the list of items needed to build the menu. This class is what I'm using to set the ViewData value in the controller. The idea being that if an application needed more control of how the menu data was generated, they could create their own class to generate the data as long as it returns a list of the correct type of objects.
This solution seems to work fine so far, it just doesn't 'feel' right for some reason. I'm hoping that I can either get some ideas of better way to do this or some reassurance that this is a valid approach to solving this problem.
If it is something that will be on every page, do something like this:
Create a base controller:
public class MyBaseController : Controller
Have this controller get the data it needs and send that data in the ViewData["menu"] to the View. Then have all your controllers inherit from this one:
public class HomeController : MyBaseController
In the Master Page, loop through your ViewData and create your menu.
(I did something like this for my sub-menu which displayed a list of categories.)
In the book I am reading (Pro ASP.NET MVC Framework by Apress) they use Html.RenderAction for the menu in the masterpage. I am a Asp.net MVC novice so maybe somebody else can give more info about this.
You can download the sourcecode at apress.com though so maybe that could help.