I tried implement web API controller for my existing web form project, i used following code for route my API.
RouteTable.Routes.MapHttpRoute(
name: "EnadocApi",
routeTemplate: "apiv2/{controller}/{action}/{Id}",
defaults: new { Id = RouteParameter.Optional }
);
But it gave following error.
Error
I used VS2015.
The first thing I'd try is to add a reference to System.Web.Http, then add that as a using in your RouteConfig class.
using System.Web.Http;
Related
In dotnet core, I'm trying to create a webapi endpoint that have a value before the controller name.
ex:
template: "api/{AccountId}/[controller]"
endpoint "api/435ABC/Reports/EndOfYear"
I've seen many examples on how to do this in MVC and in Framework 4.x, but not many with WebApi and where I set a parameter before the controller name.
In attribute routing you should change your controller route to [Route("api")] to accept all calls from https://example.com/api.
Note: it will affect all routes inside the Reports controller.
[Route("api")]
public class ReportsController : ApiController
and then decorate your action with route attribute like below:
[Route("{id}/[controller]/[action]")]
this way you can call your action method with https://example.com/api/435ABC/Reports/EndOfYear.
In convention-based routing you should only add route in UseMvc method and remove Route attributes from controller and action:
app.UseMvc(routes =>
{
routes.MapRoute(name: "default", template: "api/{controller=Home}/{action=Index}"); // this line specifies default route
routes.MapRoute(name: "Reports", template: "api/{id}/{controller=Reports}/{action=EndOfYear}"); // this line specifies your custom route
});
I have a .net core 2.0 web api with the following get method:
[HttpGet]
public async Task<IEnumerable<Customer>> Get()
{
return await customerDataProvider.GetCustomers();
}
In the startup class i have the following in configuration:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}");
});
when i run the application i get the results displayed as raw json in the browser.
I would like to add a view and to handle the results in that view meaning display them in a table and add some filtering and sorting options.
How can i achieve this? I saw different articles on how to add a view or a razor page to a project but none of them was similar to my case..
Thanks!
I have an existing ASP.NET webforms website and I have two pages that are ASP.NET MVC that I want to have coexist on the same site.
It works fine except for default documents -- I want htp://localhost to show localhost/default.aspx. Currently:
htp://localhost/ displays the MVC page which should be: htp://localhost/TestPage/Index
htp://localhost/default.aspx displays the webforms page (as it should)
There's nothing special in the RouteConfig.cs -- I'm just using this:
routes.MapRoute(name: "MVCPage",
url: "{controller}/{action}/{id}",
defaults: new { controller = "TestPage", action = "Index", id = UrlParameter.Optional } );
You can add an ignore route statement.
routes.IgnoreRoute("default.aspx");
https://weblogs.asp.net/pjohnson/mvc-s-ignoreroute-syntax
http://www.c-sharpcorner.com/UploadFile/f82e9a/ignore-route-in-mvc/
https://msdn.microsoft.com/en-us/library/dd505203(v=vs.118).aspx
I want to route any url address like /admin/* as my admin is on Angular2 and uses its routing. I tried template: "admin/{*url}"though it works not only with /admin url but with any other. Is there any way to solve this problem?
Add this code in your WebApiConfig.cs file :
config.Routes.MapHttpRoute(
name: "some name",
routeTemplate: "admin/{*url}",
defaults: new { controller = "Admin", action = "Your default action here" });
Set the default controller in defaults.
I’m going crazy here… this seems like a very simple task. First off, I know only the basics of the Web Api and MVC – so please don’t skewer me.
In the project I need to logically create controller subfolders (for organization purposes). I had a feeling it wasn’t as simple as I thought. I have the default route like this:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Which works as it should directly from the controllers folder in my project. I have added a subfolder in the controllers folder controllers/reports. I have searched quite a bit and just can't quite find a solution. How can I add a route that will direct to the subfolder.
I have tried:
config.Routes.MapHttpRoute(
name: "ReportingApi",
routeTemplate: "api/Reports/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
and:
config.Routes.MapHttpRoute(
name: "ReportingApi",
routeTemplate: "api/Reports/{id}",
defaults: new { controller = "userunit" id = RouteParameter.Optional }
Nevermind I'm an idiot... I left the default route in, removed the "Reports" in the url. It found the controller even though it was in a subfolder.