ASP.NET MVC Attribute routing: NullReferenceException on specific controller - c#

I'm working on a MVC project with several controllers that used to route with default RouteConfig, i.e. "{controller}/{action}/{id}".
Now the project leader wants to implement custom routes. I decided to use Attribute Routing because I used that on another small project which only had one controller.
I prefixed the HomeController and the AccountController and all the actions have Routing attributes now. This is working fine for Home and Account Controller. But I have another controller which I called PaymentController. Somehow, this controller cannot be reached with attribute routing. I tried prefixing and not prefixing the whole controller. What I think is the most strange thing is that it's throwing a NullReferenceException in System.Web.Mvc.Routing.DirectRouteCandidate.CreateAmbiguousMatchList.
I used the routes.MapMvcAttributeRoutes(); in RegisterRoutes, obviously, since the routing for the other two controllers works fine. Before adding the attributes in my payment controller, the default routing worked fine as well.
Do I have to configure anything else, did I forget something? Has anyone ever experienced a NullReferenceException in AttributeRouting? Is there a way to debug routing? I tried glimpse but since the routing is not successful it doesn't show anything.
Thanks in advance for your help.
edit:
As stated above, the controller is reached with default routing, so no nulref in the controller. I think the exception happens in
'System.Web.Mvc.Routing.DirectRouteCandidate.SelectBestCandidate'

Related

MVC5 / WEBAPI 2 routing HomeApiController to /api/home rather than /homeapi

I have an existing webapi 2 application that needs a basic front end adding. The existing webapi controllers have been created in the Controllers directory root named xController yController.
Controllers
-XController.cs
-YController.cs
with the following route
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}"
);
Each one of these controllers needs an accompanying MVC controller. What i would like to do is to rename the API controllers to XApiController YApiController and use routing to ensure existing usages of the service done break. Then I can add standard MVC controllers for the front end.
Controllers
-XApiController (previously XContoller)
-XController
-YApiController (previously YController)
-YController
Can you not just use the RoutePrefix attribute to do this? then you can call your controllers whatever you want and just have the attribute decide where it should be hosted, there are pros and cons to controlling your routing at the controller level but it seems to be a common use case, so for example:
[RoutePrefix("api/home")]
public class SomeHomeController: ApiController
{
// ...
}
Controllers are separate types in each framework, and each framework can discover them regardless of their location (provided they have the right name). There is no reason to mess with routing to get your desired result. The only thing you need (assuming it is acceptable) is to put your controllers into a different namespace/folder so you can have 2 controllers (MVC and Web API) with the same name.
ApiControllers
-XController
-YController
Controllers
-XController
-YController
If you ask me, it is still better to keep the MVC and API controllers in a separate location even if you cannot deploy them as separate applications.

.Net MVC 3 403.14 error

I'm creating an asp.net MVC3 demo application. I have attached the application with this thread. I'm facing a HTTP Error 403.14 - Forbidden issue when trying to run the application. The issue occurs only in a specific scenario. i.e. in the RegisterRoutes method in the global.asax
has only the below code then the issue occurs.
routes.MapRoute(
"Process1", // Route name
"Process/List" // URL with parameters
);
If we have the above code with default route the application works fine without any issues. The issue occurs when i have this particular code alone in the register routes method.
I tried the solutions given in this link but it did not work.
I assume the issue with that code. If yes then please let me know what causes the issue. I use windows 8.1 & the iis is 8.5.
The code can be found here
You have removed the default route and defined the following route:
routes.MapRoute(
"Process1", // Route name
"Process/List" // URL with parameters
);
In this declaration it is not clear which controller and which action should be
executed. You should explicitly state them:
routes.MapRoute(
"Process1", // Route name
"Process/List", // URL with parameters
new { controller = "MyController", action = "MyAction" }
);
and then make sure that you are making the request to the proper url that you have defined:
http://localhost:1619/Process/List
because you don't have any route configured that is capable of handling the http://localhost:1619/ request that you seem to have made in your screenshot.
Of course having such route is completely constraining. The only controller action you could ever have in your MVC application is the one defined as such in the route config. This is fine in a SPA application where you have a single entry point serving the SPA and a separate ASP.NET Web API but in a real ASP.NET MVC application with multiple controllers and actions it is not going to work. So you probably want to keep the default route in this case and do not experiment in this area.
You must have a default root in your global.asax file to make it work. add one default and then you can add this route.

ASP.NET MVC routing conflict

I have two routes in my application, each in a different Controller, that look like this:
[Route("forgot-password", Order = 1)]
[Route("{variable}", Order = 2)]
When I run the application I get the exception:
Multiple controller types were found that match the URL. This can
happen if attribute routes on multiple controllers match the requested
URL.
Remember these actions are in different Controllers. The Order attribute doesn't seem to work across Controllers!.
How can I get this scenario to work in asp.net mvc routing? I want to use attribute based routing and I don't want to change my urls.
The mechanism behind the error is explained here (I guess you are using Web API). Shortly put, precedence is honored only inside a controller.
Also, it is unclear for me why you want such a generic route like:
[Route("{variable}")]
The problem is that all routes in your application have been stored together. Even if it is located in different controllers, it is the same type, so they can "see" each other. In your case "forgot-password" and "{variable}" have the same format, that's why error about multiple routes has been displayed. As #NightOwl888 said, you may use RouteConfig to create a routes, but in that case you have to change your routing values.

ASP.NET WebApi specify action

I need to create ASP.NET WebApi with couple operation, for example RegisterAppStart, RegisterStep and RegisterAppEnd. And I want to place all this actions in one controller. As I read, usually in WebApi action name is not using.
And my question - is this a bad idea to rewrite default route config with actions using?
ps. sorry for my English
You can give actions arbitrary names using the [Route("/api/FooController/BarMethod"] attribute routing.
This usually overrides the "REST"yness of your service (where the request method indicates which method should be called), but as you aren't really building a REST service, you shouldn't care.

ASP.NET MVC 2 "RequiresRole" attribute does not work

I'm developing a ASP.NET MVC 2 web application. So far, I managed to define access rules for every controller function, using "RequiresRole" attribute.
Suddenly, this way of defining access rules stopped working (now every user can invoke any of the controller methods). :S. I tried debugging, and it seems that user-roles are correct. I tried reviewing web.config, but did not find anything suspicious.
Don't know what else could be the problem.
Any ideas??
RequresRoleAttribute is intended for use on WCF domain data services, not MVC controllers. I believe the attribute you should use is AuthorizeAttribute, setting the Roles parameter.

Categories

Resources