Web API2 Help Page Not working - asp.net-web-api

I have an existing ASP.NET Web API2 project. I thought of adding help page. But my controller methods are displayed only my help page, even I see my page without css applied. I can see the documentation xml file on my bin folder .

Add to project
HomeController.cs
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Title = "Home Page";
return View();
}
}

Related

Is it possible to refresh a View Component in an ASP.NET Core 2.1 Razor page?

Is it possible to refresh a View Component in an ASP.NET Core 2.1 Razor page?
I discovered that this is possible in ASP.NET MVC using AJAX—see solutions at Viewcomponent alternative for ajax refresh and ViewComponents are not async. But what about ASP.NET Core?
One solution I found is basically to have a controller returning the View Component, and then using AJAX to do the refresh.
This is example is borrowed from ViewComponents are not async
~/HelloWorldViewComponent.cs
public class HelloWorldViewComponent : ViewComponent
{
public IViewComponentResult Invoke()
{
var model = new string[]
{
"Hello", "from", "the", "view", "component."
};
return View("Default", model);
}
}
~/HomeController.cs (I cannot get this part working in razor pages)
public class HomeController : Controller
{
public IActionResult GetHelloWorld()
{
return ViewComponent("HelloWorld");
}
}
~/Views/Shared/Components/HelloWorld/Default.cshtml
#model string[]
<ul>
#foreach(var item in Model)
{
<li>#item</li>
}
</ul>
~/wwwroot/index.html
<body>
<script src="js/jquery.min.js"></script>
<script>
$.get("home/GetHelloWorld", function(data) {
$("body").html(data);
});
</script>
</body>
My problem is that I cannot get a controller working together with Razor Pages, I cannot figure out if that's even possible.
TLDR
Open Startup.cs and replace this
app.UseMvc();
with this
app.UseMvcWithDefaultRoute();
Details
The ASP.NET Core Razor Pages template (dotnet new razor) does not come with controller routes because it does not come with controllers. The easiest way to add controller routes is via UseMvcWithDefaultRoute(), which is the equivalent of the following:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
With that route installed, the URL ~/Home/GetHelloWorld maps to the HomeController.GetHelloWorld() action.
Here is a GitHub commit that demonstrates your use case in a new ASP.NET Core Razor Pages app.
See Also
https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1
To do this within the RazorPage ensure that the handler method follows the convention OGet[MyMethodName] and this should resolve your routing issue
public class HomeController : Controller
{
public IActionResult OnGetHelloWorld()
{
return ViewComponent("HelloWorld");
}
}
And modify the get request to make use of the handler
<body>
<script src="js/jquery.min.js"></script>
<script>
$.get(window.location.href.split('?')[0] + "?handler=HelloWorld", function(data) {
$("body").html(data);
});
</script>
</body>
Various example are available at https://learn.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-3.1&tabs=visual-studio

How to make an ASP.NET MVC 5 Area appear as the site root using Attribute Routing

I have moved the Visual Studio 2013 template 'Home' controller into its own Area.
I am trying to make an ActionLink that points to an action on the HomeController.
However, instead of the link being rendered as :
www.site.com/Home/ActionName
I want it to render as
www.site.com/ActionName
(For all the actions in the controller).
This way the root of my site doesn't contain 'Home' in the links.
I am trying to deploy my routes using Attribute Routing, however I am lost as to how I do this, any point in the right direction would be appreciated.
Ok, so I figured it out:
On my controller to be used as the site root (for instance "HomeController"):
[RouteArea("Home")] // Area
[Route("Home")] // Controller
public class HomeController : Controller
{
[Route("~/")] // Rendered path of the Index Action www.site.com/
public ActionResult Index()
{
return View();
}
[Route("~/About")] // Rendered path of the About Action www.site.com/About
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
// ...And with no routing attribute on the Contact Action below,
// The rendered path will remain in the format:
// www.site.com/Home/Home?action=Contact
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
ActionLink examples (from above code):
This will now render as www.site.com
#Html.ActionLink("Home", "Index", "Home")
This will render as www.site.com/About
#Html.ActionLink("About", "About", "Home")
This will render as www.site.com/Home/Home?action=Contact
#Html.ActionLink("Contact", "Contact", "Home")
I hope this helps someone else. Thanks.

mvc4 Razor Ajax Call to show partial view

Am trying to get a partial view to render with an AJAX call. The following ActionResult is in my base controller which is inherited by all other controllers in the solution:
public ActionResult FileManager()
{
return View("_FileManagerPartial");
}
and the folowing code is another partial that sits on the page
#Ajax.ActionLink("File Manager", "FileManager", new AjaxOptions { UpdateTargetId = "dvFilemanagerContainer" });
dvFilemanagerContainer is a div in the layout view and the partial view "_FileManagerPartial.cshtml" is in the shared views folder.
When I click the link for the ajax call, instead of loading the intended partial view it loads a duplicate of the page into the div.
Any ideas?
Edit
PartialView contents its currently just the following
<div id="dvFilemanagerWrapper">
File Manager
</div>
change your controller to
public PartialViewResult FileManager()
{
return PartialView("_FileManagerPartial");
}
In View add this line, so that with partial view, the master layout is not rendered, only partial view is rendered:
#{
Layout = null;
}
<div id="dvFilemanagerWrapper">
File Manager
</div>
Problem was to do with how my RouteConfig was handling the call. Have created a new MapRoute to point it to the right place and is now working. Thanks for help guys.

Return view into an Iframe with MVC3

I'm new with MVC and I need to put into an iframe a view which returns the content of a page( styles/css,text/html,images etc) and the view to containt the main menu of the application, from the Index view.
Can anyone help me with this?
The code line which returns the content in the view is :
#Html.Raw(System.Text.Encoding.ASCII.GetString(#Model.Content))
Just try to create controller with action that returns your view.
public class MyController
{
public ActionResult Index()
{
return View();
}
}
index view content:
#Html.Raw(System.Text.Encoding.ASCII.GetString(#Model.Content))
After that
<iframe src="localhost/mycontroller">
</iframe>

strange behavior of MVC3

I created a custom Model i.e. to support my Razor View. Then I created a controller as following`namespace MyCandidate.Controllers
public class CandidateViewModelController : Controller
{
//
// GET: /CandidateViewModel/
public ActionResult Index()
{
return View();
}
}
I also have the following statement in my _Layout.cshtml
#Html.ActionLink("Canid", "Index", "CandidateViewModel")
Next i created a view and the very first statement of the view is
#model MyCandidate.Models.CandidateViewModel
when i run my project i get the following error
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
I spent more than 3 hours but could not figure out?
your Index() not get any parameters, but you send "CandidateViewModel") add Index(string input) method with attribute [HttpGet] to controller.
this error meen you haven't view "Index" in "Views/CandidateViewModel/Index.cshtml".
Maybe you delete master page files (_ViewSrat, _Layout)
Or you made a mistake when you change your routes
Change your ActionLink to:
#Html.ActionLink("Canid", "Index")
If you want to pass any data to View , you can also use ViewBag:
// Controller :
ViewBag.CandidateValues = CandidateViewModelData;
// View
#Html.Label("LabelName", (CandidateViewModel) ViewBag.CandidateValues.FiledName);

Resources