I am using MVC 3 application and used web api controller.
I want to call a method with parameters .
But when I try to call the method I get Internal Server error(500)
My global.asax is:
routes.MapHttpRoute(
name: "PostMethod2",
routeTemplate: "api/mycontroller/{a}/{b}",
);
Should I add any code to application_start of Global.asax to route to the parameterized method?
Your route configuration doesn't seem legit, a proper one would look like below
routes.MapHttpRoute(
name: "PostMethod2",
routeTemplate: "api/{controller}/{action}/{a}",
);
Note, "{controller}" and "{action}" can't be random. MVC uses it to map to your controller and action(method) respectively. {a} will be mapped to your parameter "int a". MVC uses mapping by convention.
RouteTable.Routes.MapPageRoute("Profile", "{Link}.{Id}.aspx", "~/Profile.aspx");
Example be helpful!
Link and Id is argument
Related
I have created a angular2 project using ASP.net with webapi. I have everything setup correctly and working but when I am routing in angular2 such as: "localhost:1234/login" the source of the page is displaying the error:
HTTP Error 404.0 - Not Found
This is obviously because of server routing that cant find any path for this. My question is: Is there any way to disable all routing by the server except for if I'm doing a call like: "localhost:1234/api/login" because that would entail making a server request to a webapi controller which will return JSON. I want angular2 to handle all forms of routing. And the server just to handle data being pushed out.
Thank you in advance
If Angular is generating views by consuming APIs this should not be an issue. If the server is expected to serve up HTML for the routes Angular is calling then it needs equivalent routing configured.
Yes you can do this in your startup.cs class.
You need to configure the way your API calls are made in a way, and redirect to your index.html otherwise like so :
app.UseMvc(r =>
{
r.MapRoute(
name: "default",
template: "{controller}/{path?}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" }
);
r.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" }
);
});
I'm implementing a REST API using ASP.NET's Web API framework. My API routes have the typical format,
$BASE_URI/api/resource1...
$BASE_URI/api/resource2...
$BASE_URI/api/resource3...
etc. Most (if not all) routes also have an optional prefix with an ID:
$BASE_URI/api/prefix/{id}/resource1...
$BASE_URI/api/prefix/{id}/resource2...
$BASE_URI/api/prefix/{id}/resource3...
When the prefix is not supplied, the API is supposed to use a default value (like 0) and the response does not require authorization. When the prefix is supplied, the request must be authorized.
The prefix gets handled in the same way every time (but obviously the different resources get handled differently). Is there a way for me to parse the prefix component of the route separately, and then handle the route suffix normally? I am trying to avoid the situation where I have duplicate route entries for every resource, where the only difference is the prefix handling.
If you think this is bad design, I'd like to hear about that too.
Thanks in advance.
You need to define two separate routes, there's no other way around.
config.Routes.MapHttpRoute(
name: "DefaultApiWithPrefix",
routeTemplate: "api/prefix/{prefixId}/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
If you're using Web API 2, below is a guide to Attribute Routing, otherwise there is no simple work around.
http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
I am building a restful services for my data using ASP.NET web api 2.
In the data model, I have an entity called "ServiceProvider" which provide "Service"(s).
so, I am building restful for those two entities.
the requirements is to build two restful services, one as follow
api/serviceProvider/{id}/services
which gives list of services provided by that provider.
as well, the requirements is to give the same data (services by provider) through this rest service
api/services?serviceProviderId={providerId}
so, I created two controllers, for "serviceProvider", and for "Service"
I already implemented the second call, by creating an action method in the "Service" controller with this signature
public IEnumerable<Service> GetServiceByProvider(int providerId)
because both are going to run the same code, can I just re-route the first call to the second one?
At the same time, not effecting the other actions in both controllers??
I tried to do this is the webApiConfig with no success
config.Routes.MapHttpRoute(name: "serviceByProvider",
routeTemplate: "api/serviceProvider/{id}/services",
defaults: new { controller = "Service", action = "GetServicesByProvider" });
where the "GetServiceByProvider" is the method name in the "Service" controller that will serve the second call
but without success
As I said there are other actions in both controllers I don't want to affect them by this routing.
Like I have the following
api/serviceProvider
api/serviceProvider/{id}
api/services/
api/services/{id}
.....
I decided to try again and duplicate your environment and found a problem in your route template. The parameter your route is expecting is called id but your GetServicesByProvider method accepts a parameter called providerId. The framework is looking for parameters with the same name. Changing this should get your routing working the way you want it to AND maintain all of your previous routes.
config.Routes.MapHttpRoute(
name: "serviceByProvider",
routeTemplate: "api/serviceProvider/{providerId}/services",
defaults: new { controller = "Service", action = "GetServicesByProvider" }
);
I'm trying to learn learn how routing works in Symfony2, and so far everything I've read has examples like this:
blog:
path: /blog/{page}
defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 }
This routes requests to /blog/123 to the AcmeBlogBundle Blog controller's "index" action, and passes the 123 parameter as the "page" parameter to that controller action. If no page parameter is passed, then the page defaults to 1.
That's all well and good, but what if you want to simply have a convention based routing system that passes things through like this:
/{bundle}/{controller}/{action}
So, for a URL like this:
/acme/blog/index
It would then call AcmeBlogBundle Blog controller's "index" action.
No specific routing configuration is necessary, it simply infers the bundle, controller, and action from the URL. So you can continue adding bundles, controllers, and actions, and you don't need to modify the routing configuration. It just works.
If this isn't possible, can you at least infer the controller and action from the URL? E.g., perhaps you need a route that specifically identifies the bundle, but can we get the controller and action from the URL?
I read through the Symfony "The Book" page about routing, and I couldn't figure out a way to do this.
No way. This was considered as bad practice and so it was removed from symfony.
But you should take a look at the #Route annotation, as it simplifies configuring routes in such a nice way. Directly attached to the action, there is no lack between config and code.
Good morning everyone,
I have a question about J2EE framework.
since ASP.net 3.5 mvc add a new feature "Routes" do we have similar function in j2ee ?
here is some text about this feature in asp.net3.5
Routes are a new feature in .NET 3.5 SP1, and ASP.NET MVC uses this feature to give controller classes the capability to respond to requests. ASP.NET MVC uses REST-like URLs (Representational State Transfer) that are cleaner than regular ASP.NET web application URLs. Here are examples of such URLs:
/products/show/881
/customers/list
/login/register
As you can see, REST-like URLs tend to be clean, simple, and don't expose .aspx files directly on the server. Although you can have directly addressed .aspx pages in ASP.NET MVC applications, this is not the main idea.
Through developer-defined routes, the ASP.NET MVC application can direct requests to controllers. Routes are defined once for the entire ASP.NET application, so the Global.asax file is the logical place to define these. In fact, if you take a look at the default Global.asax.cs file in an ASP.NET MVC application, you will see a method called RegisterRoutes defined. This method is called from the Application_Start method, and implemented like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home",
action = "Index",
id = "" }
);
}
By default, a route called "Default" is defined with three parts: controller, action, and finally an ID separated by slashes. When a request arrives at the MVC application, the URL is parsed according to this definition. For instance, given the request for "/products/show/881", this would be parsed so that the "controller" parameter would have the value of "products", the "action" parameter would be "show", and finally the "id" would be "881".
If you use Spring, you can use it's #RequestMapping annotations to achieve something similar, so you could have something like
#Controller
#RequestMapping("/products")
public class ProductController {
#RequestMapping("/show/{id})
public String show(#PathVariable Integer id) {
.......
}
}
And if you want to make sure that jsps can't be directly called, you place them inside WEB-INF