attributerouting

Attribute routing with optional parameters in ASP.NET Web API

我的未来我决定 提交于 2019-11-30 07:02:44
问题 I'm trying to use Web API 2 attribute routing to set up a custom API. I've got my route working such that my function gets called, but for some reason I need to pass in my first parameter for everything to work properly. The following are the URLs I want to support: http://mysite/api/servicename/parameter1 http://mysite/api/servicename/parameter1?parameter2=value2 http://mysite/api/servicename/parameter1?parameter2=value2&parameter3=value3 http://mysite/api/servicename/parameter1?parameter2

How to find the right route in a RouteCollectionRoute?

a 夏天 提交于 2019-11-30 02:30:03
问题 I am testing ASP MVC routes. I am having an issue with attribute routes in ASP MVC 5.1 When I have a controller like this: public class FooController : Controller { [Route("foo")] [HttpGet] public ActionResult Get() { .... } [Route("foo")] [HttpPost] public ActionResult Post() { .... } } Then in order to test which route matches a particular request, I call routes.GetRouteData. I get a System.Web.Routing.RouteData that contains the route as well as values that should say which controller and

ASP.NET Help Pages default home page?

僤鯓⒐⒋嵵緔 提交于 2019-11-29 22:59:33
I want to go to http://myserver and be able to get Help Pages as the default home page, so the first thing a guest to http://myserver should see is the Help Page. I have a default route set up like this: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } Then I have my Help Page Area registration set up like this: public override void RegisterArea(AreaRegistrationContext context)

CreatedAtRoute routing to different controller

非 Y 不嫁゛ 提交于 2019-11-29 22:53:09
I'm creating a new webapi using attribute routing to create a nested route as so: // PUT: api/Channels/5/Messages [ResponseType(typeof(void))] [Route("api/channels/{id}/messages")] public async Task<IHttpActionResult> PostChannelMessage(int id, Message message) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != message.ChannelId) { return BadRequest(); } db.Messages.Add(message); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = message.Id }, message); } I however want to return a route that isn't nested i.e.: /api/Messages/{id} which is defined

Multiple controllers with same URL routes but different HTTP methods

房东的猫 提交于 2019-11-29 19:30:30
问题 I've got a following two controllers: [RoutePrefix("/some-resources") class CreationController : ApiController { [HttpPost, Route] public ... CreateResource(CreateData input) { // ... } } [RoutePrefix("/some-resources") class DisplayController : ApiController { [HttpGet, Route] public ... ListAllResources() { // ... } [HttpGet, Route("{publicKey:guid}"] public ... ShowSingleResource(Guid publicKey) { // ... } } All three actions got in fact three different routes: GET /some-resources POST

Is there a way to have a RoutePrefix that starts with an optional parameter?

给你一囗甜甜゛ 提交于 2019-11-29 14:05:36
I want to reach the Bikes controller with these URL's: /bikes // (default path for US) /ca/bikes // (path for Canada) One way of achieving that is using multiple Route Attributes per Action: [Route("bikes")] [Route("{country}/bikes")] public ActionResult Index() To keep it DRY I'd prefer to use a RoutePrefix, but multiple Route Prefixes are not allowed: [RoutePrefix("bikes")] [RoutePrefix("{country}/bikes")] // <-- Error: Duplicate 'RoutePrefix' attribute public class BikesController : BaseController [Route("")] public ActionResult Index() I've tried using just this Route Prefix: [RoutePrefix(

Getting “No type was found that matches the controller named 'SampleSlashBaseService'” when trying to use WebAPI

▼魔方 西西 提交于 2019-11-29 09:11:15
I have a webapi project with a base ApiController named SlashBaseService: [RouteArea("uBase")] public abstract class SlashBaseService : ApiController { } The resulting dll is used in a WebForms project so I also have a WebActivator class with the following code to generate routes: RouteTable.Routes.MapHttpAttributeRoutes(config => { // Get all services inheriting from SlashBaseService foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (var type in assembly.GetTypes()) { if (type.IsSubclassOf(typeof(SlashBaseService))) { // Scan assembly config.ScanAssembly(assembly); /

Attribute routing with optional parameters in ASP.NET Web API

无人久伴 提交于 2019-11-29 00:55:48
I'm trying to use Web API 2 attribute routing to set up a custom API. I've got my route working such that my function gets called, but for some reason I need to pass in my first parameter for everything to work properly. The following are the URLs I want to support: http://mysite/api/servicename/parameter1 http://mysite/api/servicename/parameter1?parameter2=value2 http://mysite/api/servicename/parameter1?parameter2=value2&parameter3=value3 http://mysite/api/servicename/parameter1?parameter2=value2&parameter3=value3&p4=v4 The last 3 URLs work but the first one says "No action was found on the

Web API. Generic Controller with attribute routing

£可爱£侵袭症+ 提交于 2019-11-28 11:01:29
问题 The problem When I create a method with a route on the actual controller, say: "api/country/something"... When I perform the above request, my code gets executed & data gets returned. But when I try call my route on the base controller. E.G: "api/country/code/123" I get a 404 error. Question Any idea on how to implement generic routes whilst making use of attribute routing? Specific Controller [RoutePrefix("Country")] public class CountryController : MasterDataControllerBase<Country,

Is there a way to have a RoutePrefix that starts with an optional parameter?

China☆狼群 提交于 2019-11-28 07:33:01
问题 I want to reach the Bikes controller with these URL's: /bikes // (default path for US) /ca/bikes // (path for Canada) One way of achieving that is using multiple Route Attributes per Action: [Route("bikes")] [Route("{country}/bikes")] public ActionResult Index() To keep it DRY I'd prefer to use a RoutePrefix, but multiple Route Prefixes are not allowed: [RoutePrefix("bikes")] [RoutePrefix("{country}/bikes")] // <-- Error: Duplicate 'RoutePrefix' attribute public class BikesController :