asp.net mvc - How to define routing for simple subfolder/action case? -
i make simple asp.net mvc application (or call hhtp service) can post simple data using post method. want use following url:
www.mysite.com/api/postdata
yet havenit figured out how configure routeconfig , contoller. best attempt was, , have tried dozens others is:
public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( name: "apipostdata", url: "api/postdata", defaults: new { controller = "api", action = "postdata" } ); } my controller:
public class apicontroller : controller { //[httppost] public jsonresult postdata(postdata postdata) { // save database retun json(true); } } type postdata declared simpler object.
why not work? error 404. have tried many many variations around that. read hundreds of pages, yet not single working example. have came across called areas refuse believe coding , complications necessary such trivial task. cannot true.
edit - update:
i have prepared more trivial test case:
public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); // executes, checked in debugger routes.maproute( name: "test", url: "api/test", defaults: new { controller = "api", action = "test" } ); } public class apicontroller : controller { public actionresult test() { return content("test test test"); } } so, if call with
localhost/site/api/test i should "test test test" response. wrong !!! is
no http resource found matches request uri 'http://localhost/site/api/test'.no type found matches controller named 'test'.
what going on ????
update
i have in desperation tried direct version no "folder".
public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( name: "test2", url: "test", defaults: new { controller = "api", action = "test" } ); } and can call with
www.mysite.com/test and returns test test test should.
update
it seems bug. if reconfigure above maproute target
www.mysite.com/xxx/test
it works. seems if url contains string "api" breaks.
try route:
routes.maproute( name: "apipostdata", url: "api/{action}/{postdata}", defaults: new { controller = "api", action = "postdata", postdata = urlparameter.optional } ); this match request "www.mysite.com/api/postdata/12345" example.
Comments
Post a Comment