Why won't my call to a Web API route to the correct method? - asp.net-web-api

I have my first Web API working, but the even when I call it and pass an an id is gets routed to the method that has no arguments.
Here is my controller code:
public class ChartController : ApiController
{
Chart[] _charts = new Chart[]
{
new Chart { Name = "Chart 1" },
new Chart { Name = "Chart 2" },
new Chart { Name = "Chart 3" }
};
// GET api/chart
[Route("api/chart")]
public IEnumerable<Chart> Get()
{
return _charts;
}
// GET api/chart/{id}
[Route("api/chart/{id}")]
public IEnumerable<Chart> Get(int chartId)
{
Chart[] charts = new Chart[]
{
Charts.Database.ChartsDB.GetChart(chartId)
};
return charts;
}
}
Here is my routing in my global.asax
RouteTable.Routes.MapHttpRoute(
name: "ChartApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
This is my request uri
http://localhost:42311/api/chart
And the results
[
{
"Name": "Chart 1"
},
{
"Name": "Chart 2"
},
{
"Name": "Chart 3"
}
]
When I change the uri to
http://localhost:42311/api/chart/1
I get the same results, as both calls are routed to
public IEnumerable<Chart> Get()
What am I doing wrong?

Please note that WebApi works based on reflection this means that your curly braces {vars} must match the same name in your methods.
Therefore to match this api/chart/{id} your method needs to be declare like this:
[Route("api/chart/{chartId}"), HttpGet]
public IEnumerable<Chart> Get(int chartId)
return userId;
}
Where the parameter {id} was replaced by chartId.
Another option can be:
[Route("api/chart/{id}"), HttpGet]
public IEnumerable<Chart> Get(int id)
return userId;
}
If you want to read more about this Routing Rules here is similar post on this;
WebApi Routing Configuration

Related

Inconsistent culture - decimal separator ignored in model binding between razor view and viewmodel

I have the following behaviour in my program:
User input for a decimal variable
A) jquery validation turned off:
1) If the user uses a comma as decimal separator, the value is stored correctly in the ViewModel
2) If the user uses a point as decimal separator, the value is multiplied by 100 (as if there was no decimal separator)
B) jquery validation turned on:
1) I get an error, that a number must be supplied
2) same Problem as A2)
However if I display a decimal value of the ViewModel in the view it is shown per default with a point as a decimal separator.
This inconsistency is confusing me and I would like to implement a consistent behaviour, but unfortunately I don't know what I am actually looking for.
The website will be localized in german and italian. The localization works without any problems, so far.
This is my
startup.cs
namespace XXX
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Added - uses IOptions<T> for your settings.
services.AddOptions();
// Added - Confirms that we have a home for our DemoSettings
services.Configure<DatabaseSettings>(Configuration.GetSection("DatabaseSettings"));
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddAuthentication(IISDefaults.AuthenticationScheme);
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStatusCodePagesWithReExecute("/Error/Index", "?i_statusCode={0}");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
IList<CultureInfo> supportedCultures = new List<CultureInfo>
{
new CultureInfo("de"),
new CultureInfo("it"),
};
var localizationOptions = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("de"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
var requestProvider = new RouteDataRequestCultureProvider();
localizationOptions.RequestCultureProviders.Insert(0, requestProvider);
app.UseRouter(routes =>
{
routes.MapMiddlewareRoute("{culture=de}/{*mvcRoute}", subApp =>
{
subApp.UseRequestLocalization(localizationOptions);
subApp.UseMvc(mvcRoutes =>
{
mvcRoutes.MapRoute(
name: "defaultLocalized",
template: "{culture=de}/{controller=Contract}/{action=Index}/{id?}");
mvcRoutes.MapRoute(
name: "error",
template: "Error/Index",
defaults: new { controller = "Error", action = "Index", culture = "de" });
mvcRoutes.MapRoute(
name: "default",
template: "{*catchall}",
defaults: new { controller = "Home", action = "Index", culture = "de" });
});
});
});
}
}
}

mvc core2.0 routing is not Workin

Here i'm new to mvc core2.0 please help me why my Routing Is not working
My Routing Class
public static class ApplicationRoteProfiler
{
public static void Routeing(IRouteBuilder builder)
{
builder.MapRoute("route1", "", new
{
Controllers = "Department",
Action = "Add",
});
builder.MapRoute("route2", "Department/Add", new
{
Controllers = "Department",
Action = "Index"
});
}
This class file i register in startup.config file
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
app.UseMvc();
app.UseMvcWithDefaultRoute();
app.UseMvc(routes =>
{
ApplicationRoteProfiler.Routeing(routes);
});
}
When i hit my server as http://localhost:1588/Department/Add its should redirect to Department/Index But its hitting Department/Add
Should it be just Controller not Controllers??
builder.MapRoute("route1", "", new { controller = "department", action = "index" });
My 2 cents
You shouldn't use app.UseMvcWithDefaultRoute() and app.UseMvc() at the same time. You only need to pick 1 of them.
I don't see benefits of using a static class to configure routing for MVC. You can just put all the route configurations right there inside UseMvc lamba function. Also I don't think you need to put customized route specifically for your "route1" as it follows the standard MVC routing convention.
app.UseMvc(routes =>
{
// The order of these routes matters!
routes.MapRoute(
name: "route2",
template: "department/add",
defaults: new { area = "", controller = "department", action = "index" });
routes.MapRoute(
name: "default",
template: "{controller=home}/{action=index}/{id?}");
}
You can also return RedirectToAction("index"); inside your Department controller Add method so whenever /deparment/add route is hit, it redirects to /deparment/index, assuming you have the default MVC routing setup, either use the "default" route I put on #2, or use UseMvcWithDefaultRoute(). That way you don't need to create custom routes just for redirecting.
public class DepartmentController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult Add()
{
return RedirectToAction("index");
}
}

how to route custome url in MVC

I have return my Controller with attribute routing like below.
[Route("{CourseName}/{CourseCode}")]
public ActionResult getAllProductList(string CourseName,string CourseCode)
{
ViewBag.CourseName = CourseName;
ViewBag.CourseCode = CourseCode;
return View("CoursePage");
}
it works fine. but if there is any ajax method calls (for ex. ../controllername/methodname) from JS then its hitting my above controller/Action instead of "methodname" . Please suggest.
My RouteConfig Code:
routes.MapRoute(
name: "CoursePage",
url: "{CourseName}/{CourseCode}",
defaults: new { controller = "Course", action = "getAllProductList" },
constraints: new { CourseName = "\\d +", CourseCode = "\\d +" }
);

OData Action Parameters is null in run time

I was trying to define a Post OData Web API action.The parameter is a number.
I found an article
And I followed as the article described, here is the source code:
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.MapODataServiceRoute("odata", "odata", model: GetModel());
}
private static Microsoft.OData.Edm.IEdmModel GetModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<ThumbnailQueue>("ThumbnailQueues");
builder.EntitySet<Document>("Documents");
EntitySetConfiguration<ThumbnailQueue> thumbnailQueue = builder.EntitySet<ThumbnailQueue>("ThumbnailQueues");
var action = thumbnailQueue.EntityType.Action("PostThumbnailQueue");
action.Parameter<long>("DocumentSN");
var postThumbnailQueue = thumbnailQueue.EntityType.Collection.Action("PostThumbnailQueue");
postThumbnailQueue.Parameter<long>("DocumentSN");
return builder.GetEdmModel();
}
}
Web API
public class ThumbnailQueuesController : ODataController
{
private MarketingEntities db = new MarketingEntities();
// POST: odata/ThumbnailQueues
[HttpPost]
public IHttpActionResult PostThumbnailQueue(ODataActionParameters parameters)
{
if (parameters == null) return BadRequest();
var documentSN = (long)parameters["DocumentSN"];
if (db.ThumbnailQueues.Any(t => t.DocumentSN == documentSN))
{
return BadRequest("Record already exists");
}
var myThumbnailQueue = new ThumbnailQueue();
myThumbnailQueue.DocumentSN = documentSN;
myThumbnailQueue.Status = 1;
db.ThumbnailQueues.Add(myThumbnailQueue);
db.SaveChanges();
return Ok();
}
I used Postman to test the function
But in the run time, the parameter is null, so I can not get documentSN from the parameter. I have troubleshoot this porblem for hours, could anybody help me to fix this problem? Many thanks~
You need to use the following route for that: http://localhost:10076/odata/ThumbnailQueues/Default.PostThumbnailQueue
ThumbnailQueues - entity set
Default - default namespace
PostThumbnailQueue - action

ExtDirectSpring Router URL?

I'm trying to get ExtDirectSpring to work but can't figure out what the URL to the Direct Router shall be.
My controller:
#Controller
#RequestMapping(value = "/sonstiges/extdirect")
#Transactional
public class ExtDirectController {
#RequestMapping(value = "")
public String start() throws Exception {
return "/sonstiges/extdirect";
}
#ExtDirectMethod(value = ExtDirectMethodType.STORE_READ)
public List<String> loadAllMyStuff() {
return Arrays.asList(new String[] {"one", "two"});
}
}
In my JSP I add a provider like this:
Ext.direct.Manager.addProvider({
"type":"remoting", // create a Ext.direct.RemotingProvider
"url":"/fkr/app/controller/extjs/remoting/router", // url to connect to the Ext.Direct server-side router.
"actions":{ // each property within the actions object represents a Class
"ExtDirectController":[
// array of methods within each server side Class
{
"name":"loadAllMyStuff", // name of method
"len":0
},
{
"name":"myTestFunction", // name of method
"len":0
}
]
},
"namespace":"FKR"// namespace to create the Remoting Provider in
});
... and use the following store to populate a grid:
store: {
model: 'Company',
remoteSort: true,
autoLoad: true,
sorters: [
{
property: 'turnover',
direction: 'DESC'
}],
proxy: {
type: 'direct',
directFn: FKR.ExtDirectController.loadAllMyStuff
}
}
Loading the page, a request to http://localhost:8080/fkr/app/controller/extjs/remoting/router is send, but not to my loadAllMyStuff- function. My guess is that the URL to the direct router is wrong.
What is the correct URL to the router?
EDIT: I just figured out that the method router in RouterController expects the parameters extAction and extMethod, but with the given code the parameters action and method are sent. That seems to be odd ...

Resources