Correct terminology and examples for nesting/routi

2019-09-09 12:52发布

问题:

I have been doing ASP.NET for a while, and have done very little in MVC (in general), but very new at the ASP.NET MVC framework, as well as the correct terminology on a few things.

Here is my example (actual application I am working on is different, but this is a public example I can use) - I want to create a simpler version of Redmine, but in .net.

I want to list issues, in such a way that if I go to example.org/issues, I see a list of all issues (similar to http://www.redmine.org/issues), across all projects.

But if I go to the project, as in example.org/project/issues, I see just the issues for that project (similar to http://www.redmine.org/projects/redmine/issues). This would be the same for example.org/project2/issues, etc.

What is the correct term for this? I would assume that the developers didn't rewrite the 'issues' code in two places, that they reused this code.

Since I don't know the fully correct term for this, it is hard to find good examples in the ASP.NET MVC world that I can start with. So the second part of this, is what would be an example that I could look at in ASP.NET MVC, and how should this look in Visual Studio to me?

There would also be several other things under /project/, like settings, details, logs, etc, similar on how one would navigate http://www.redmine.org/projects/redmine/.

Finally, what if projects was not on the top level? As in, what if my application was more like:

example.org/
example.org/projects/
example.org/projects/project1
example.org/projects/project1/issues
example.org/projects/project2
example.org/dashboard/

回答1:

Set up one controller called whatever you'd like (I'll use RedMineController).

In that controller you'll have a single action method named ListIssues. Accept a parameter named ProjectName:

public ActionResult ListIssues(string projectName) {}

Lastly create two routes in your global.asax.cs:

routes.MapRoute(
    "Issues Root",
    "issues",
    new { controller = "RedMine", action = "ListIssues" }
);

routes.MapRoute(
    "Project Issues",
    "projects/{projectName}/issues",
    new { controller = "RedMine", action = "ListIssues" }
);

In your ListIssues action, check if projectName == null, and if so get all issues, otherwise get specific ones. The first route will pass null, the second will pass what is in the URL. Don't forget to throw a 404 if the project name in the URL is invalid.

Hope this helps!



回答2:

well though not a proper ans. but you can do what you want easily by just reversing the way you are doing them.. ie
instead of doing www.example.org/project/issues do www.example.org/issues/projector just www.example.org/issues
this way you can easily make a issues controller and everything in the url after issues can be this string by which you would be able to determine the resource for which you need to list all the issues. an example route for it can be

"{issues}/{*resource}",new{controller="issues"}