New to ASP MVC 6 (and ASP in general) and was looking for some guidance.
Currently I've got a default route setup in this fashion:
public void Configure(IApplicationBuilder app)
{
app.UseMvc(routes => routes.MapRoute(
"Default", "{controller=Home}/{action=Index}/{id?}"));
app.UseFileServer();
}
In my application, I'm looking to have a few routes:
/projects/{id?}
- Returns Index if id is empty/invalid or uses a controller Projects(id)
/projects/{id?}/phase/{id?}
- Returns Project(id) if phase id is empty/invalid or uses a controller Phase(id).
Any ideas on how I could best set this up?
Thanks, Joe
I'm not an expert on routes, but this should work:
app.UseMVC(routes => {
routes.MapRoute(
name: "project",
url: "project/{id}",
defaults: new { id = UrlParameter.Optional, controller="Project", action="Index"});
routes.MapRoute(
name: "projectphase",
url: "project/{id}/phase/{pid}",
defaults: new { id = UrlParameter.Optional, pid=UrlParameter.Optional, controller="Phase", action="Index" });
});
For the invalid/empty pid, just use RedirectToAction() to send the user back to the other route.
One way to solve it is that to create a route to accept both Project ID and Phrase ID, then do the condition within any controllers
In your routeConfig.cs, create a new route:
routes.MapRoute(
name: "MyCustomRoute",
url: "{controller}/{action}/{projectID}/{phraseID}",
defaults: new { controller = "Home", action = "Index", projectID = UrlParameter.Optional, phraseID= UrlParameter.Optional }
);
Then for example, you have a controller named project
public ActionResult CheckID(int projectID, int phraseID) {
ViewBag.projectID= projectID;
ViewBag.phraseID= phraseID;
if (...check my projectID is valid...) {
... return to your project view ...
} else {
... return to your phrase view ...
}
}
The actual URL would be something like this
http://mydomain/Project/CheckID/1/2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.