How can I map different routes to the same ApiController? - asp.net-web-api2

I have a UsersController in my ASP.NET Web Api 2.2 project and I would like to expose it directly as well as nested.
For example:
/api/users
and
/api/accounts/{id:guid}/users
The first route would be used to deal with all users and the second would be used to limit the scope to the users associated with that particular account id.
I've been using [RoutePrefix("api/users")] for my routing, but it won't let me apply multiple RoutePrefix attributes. How do I accomplish this without being able to use multiple RoutePrefix attributes on the same controller?

Why not having two controllers classes: UserController and AccountController? Each one with its own RoutPrefix attribute. The first one with RoutePrefix[("api/users")] and the second one with RoutePrefix("api/accounts").

Related

Net 5 Domain restricted routes

I'm using Net 5 and I want to set up a new domain on our application.
What I'd like to happen is to be able to create the following routes:
www.domaina.com/aboutus
www.domainb.com/aboutus
I'd like these 2 routes to go to different views.
I'd also like to be able to restrict certain pages e.g.
www.domaina.com/pageA wouldn't be accessible from www.domainb.com/pageA and vice versa.
I've tried to decorate my methods and controllers with the new "Host" attribute but that doesn't seem to retrict them.
I've also tried methods from here to no avail: Domain-based routing in ASP.NET Core 2.0
I should also point out that the plan is to have these behind Azure Front Door, so I need to pull from the header "X-Forwarded-Host". I have logic to do this already in place, but would need something I can customise.
I'm using attribute based routing at the moment.
Any help would be greatly appreciated.
Thanks,
David

Custom path for resource route in react-admin

Is there any way to specify custom paths for resources?
Example: <Resource name="User" path="/manageUsers" {...}>
If it's not possible "per resource", can we for example have all the CRUD pages be under a same basepath like: /crud/{/$resource.name} but keep the custom routes without that basepath.
Thank you very much.
EDIT #1
For context, we are building an admin panel that will have a lot of flows, basically step-by-step creation of resources. So I applaud the react-admin library for what it does (manage the CRUD part), but I want more flexibility in how the URLs are going to be.
I will need to have a section called /manageUsers/ that will have some data like a dashboard, and then the list could be /manageUsers/list/.
And then I may need another section called /customers/ that would list directly on that page.
EDIT #2
To give another use case, I'm trying to use graphQL as the dataProvider, and I had to rename all my resources since the rest API is using users where as the graphQL resource is User. So all my urls are now different!
I hope that makes it a bit more clear.
Is there any way to specify custom paths for resources?
No, this is not supported at the moment. One way to tackle this would be to use manageUsers as the resource name and translate it to User in your dataProvider.
I will need to have a section called /manageUsers/ that will have some data like a dashboard, and then the list could be /manageUsers/list/.
Definitely not supported by default either. However, you can replace the default Resource component with your own. It actually just creates routes for the resource inside a React Router switch. Note that you'll probably have to override the redirect prop of some components (such as Edit) because, when passed list, they expect the list to be at the resource root path.
I had to rename all my resources since the rest API is using users where as the graphQL resource is User
That's the dataProvider job to translate resources names into what your backend expect. Use resource names that make sense for your users (url wise).

How to find YII routes from url

I am new in Yii Framework. I am doing one small correction in Yii framework web app. Their third party Fax(Fax.de) API providers change something, I am trying to change the code, but I couldn't get the right file.
If the URL is www.example.com/paymentoption. What controller will work and where can I get Route folder?
Please help me...
in yii there is no route folder or file.
From Url you can simply find the controller named paymentoption. and in that you can find the method actionIndex which is called by Default
From documentation:
Routing involves two steps:
the incoming request is parsed into a route and the associated query
parameters; a controller action corresponding to the parsed route is
created to handle the request. When using the default URL format,
parsing a request into a route is as simple as getting the value of a
GET query parameter named r.
When using the pretty URL format, the URL manager will examine the
registered URL rules to find matching one that can resolve the request
into a route. If such a rule cannot be found, a
yii\web\NotFoundHttpException exception will be thrown.
Once the request is parsed into a route, it is time to create the
controller action identified by the route. The route is broken down
into multiple parts by the slashes in it. For example, site/index will
be broken into site and index. Each part is an ID which may refer to a
module, a controller or an action.
In this case, the link: www.example.com/paymentoption will use the controller PaymentoptionController and actionIndex, or it could be a module.
I recommend reading the documentation on the part of the Routing and Url manager: https://www.yiiframework.com/doc/guide/2.0/en/runtime-routing
Yii is a conventional based framework. That is, You should follow certain naming conventions to create your component.
For example. Your controller should be
SomenameController
Notice, above controller name has naming convention Controller adjoined to Somename
Here, You don't have folder to router, but controllers
//Get controller name
echo Yii::$app->controller->id
You can find a controller with that name in your controllers folder.

Mixing Web Api and ASP.Net MVC Pages in One Project

How do I mix Web API and ASP.Net MVC pages in one project?
For instance, I have model User. I would like, within the same project, to have an ApiController that would respond to all the HTTP verbs for managing the User entities, and at the same time have a Controller that would return the appropriate strongly-typed Views depending on the Action requested.
I can't name both controllers UserController. What is the best way around this? Should I name one UserApiController and the other UserController? Any other suggestions?
You can put them in separate namespaces, e.g MyApp.Controllers.UsersController and MyApp.Controllers.WebAPI.UsersController.
This would let you expose similar URI routes in MVC and WebAPI, eg:
/users/1 << MVC view
/api/users/1 << Web API
I haven't changed the namespaces, the only thing that I had to do was register WebApi first, and then MVC route
//First register WebApi router
GlobalConfiguration.Configure(WebApiConfig.Register);
//and register Default MVC Route after
RouteConfig.RegisterRoutes(RouteTable.Routes);
and everything works great!
The WebApi implementation should be added as a separate Area in the MVC application. It is a natural fit. Doing this gives you the separate namespace that Mike Wasson recommended, plus it gives you a natural way to set up the /api routing. You get a separate model folder
Additionally, it is very specifically separated from the rest of the project. If requirements in the future are ever such that you need to separate the api implementation into a separate project, having the api implementation isolated to a separate area makes that breaking it out a lot easier.

Rails3 namespace sharing model

I want to divide my app in two namespaces, Cms and Front. Based on that I will have for example scaffold in Front named User which will handle users data of those who register in service. But the thing is, that in Cms I would also like to have acces and be able to manage users like block action, delete, resend invitation, send email etc. .
What would be the best solution for that. Should I make two separate scaffolds one in Cms another in Front and handle with them the same table in DB or should I create some Module to handle common actions for both namespaces ? Of course for each namespace I will have different DB prefix, how to work it around ?
just don't add a namespace for the common stuff. or you could also add a Common namespace to them.