What is the difference between RouteArea and RoutePrefix using AttributeRouting - c#

So I have been using http://attributerouting.net/ and love it. However, I cannot seem to wrap my head around when I should use RouteArea, or RoutePrefix, or both.
For instance, my thought is that with an API controller, use RouteArea("api"), and then for a prefix maybe use the main function of the controller? Any clarification would be helpful, thank you!

RouteArea is meant for use with MVC controllers, which have a formal concept of areas. In web api, just use RoutePrefix.

Related

Authorization/Authentication in asp.net web api

I need to create a couple of simple methods using asp.net web api. I am very new to this so even the simplest thing is proving difficult for me. I want to decorate my api controller with [authorize] and [authenticate] for the obvious reasons.
My question is centred on how I call these methods from my ajax calls (which is how I'm intending to call them). I'm reading that I need to pass the user id and password with each of my calls but how do I do this using ajax? Or do I even need to do this manually or asp.net somehow does it for me (if that even makes any sense)? Because when I'm doing action calls that need to be authenticated/authorized using forms in mvc, I certainly don't do it expressly and I imagine somehow somewhere in the code it just gets done.
As is clear, I'm very lost. Any help is appreciated.

Build an API for Multiple

I'm building an API using WebAPI that will be accessed via AJAX calls. However, the API controller will need more than just one POST method. I understand I can specify {action} in my routing, but because I've seen that this is not recommended - am I using the right tool? So 2 questions:
Is Web API the best tool for this, or is there something else I should be using?
Why should I not use more than one POST method in a WebApiController? Is including {action} in my routing a good enough solution to this problem?
1. Is Web API the best tool for this, or is there something else I should be using?
I think WebAPI is a fine choice for you, regardless of whether you have one or many POST calls per controller.
2. Why should I not use more than one POST method in a WebApiController?
To remain RESTFul you'll want a controller per entity type. Without getting too deep into details, a POST against a specific type of entity should be the 'ADD entity' call, and why would you have more than one of those? Having said that, you don't have to be fully RESTFul... if your requirements suite a multi-POST model then go for it, you can always refactor later if necessary.
...Is including {action} in my routing a good enough solution to this problem?
Again, if your goal is to be RESTFul then this isn't a great practice. However, if you have needs that are best achieved using action routings then go for it. REST is not the only model.

Find all places a method is used in my views

Sprinkled throughout my views I make calls to a method that translates text into a user's preferred language. For example:
#Translate("Translate this for me")
Now I want to find every call to this method so that I can compile a list of all the text that needs to be translated in my MVC project.
I've used reflection before to access all of my controllers and action methods, but how can I access the views and the method calls therein? Or is there another way to do this without using reflection? Perhaps the razor engine can be called upon to help with this somehow?
EDIT:
The "Translate" method is in a BaseWebViewPage that all my views inherent from
UPDATE:
Thanks for your comments, I hadn't thought of using 'find' in Visual Studio for this - it's a really practical answer, much appreciated. Although, while we're here, I wonder if there is a programmatic way to do this?

Getting rid of the /Home path name in ASP.NET MVC

I’ve only just started using ASP.NET MVC, and I have a somewhat trivial question: it seems that each controller has an attached folder-like path, so that my site becomes mydomain.net/Home/something. Is it possible to somehow get rid of the /Home part, so that the Home controller becomes ‘default’ for my web site and it’s possible to just use mydomain.net/something instead?
Sure, just define a route like so:
routes.MapRoute("{action}/{id}", new {controller="Home", action="Index", id=""});
The only problem is what about requests for your other controllers? For example, is
/Product/Foo
A request for HomeController.Product("Foo") or ProductController.Foo()?
You might need to use constraints to make the distinction clear.
I did a blog post on a simple way to handle this. I created a route constraint that selects a controller that you want to use for the root of your site. Here is the blog post if you're interested.

Could we use with and without extension on different actions in MonoRail?

I would like to build a web application on Castle MonoRail, I was wondering how can we use an action with extension and another action without extension? How can HTML helper generator url for us?
Ex:
http://mysite.com/Products/list
http://mysite.com/Products/abc.castle
The answer is Yes, you can, by using routing.
The exact method you need to use to get the routing working depends upon which version of Monorail you are using. However, adding routing can have some negative impact on performance unless you are careful.
It is also worth noting that you don't have to use the extension ".castle" or ".rails" in case this is what is distasteful to you.
You can try creating a routing role like
/<controller>/<action>.castle
and it would do the trick.
I don't see a need for this. Why would you want to access an action without using .rails or .castle?

Categories

Resources