ASP.NET Core Routing with 2 Referenced Projects - asp.net-mvc

I've added a second project to an existing solution and am running into a bit of an issue with routing requests.
When I initially add the second project, there is no problem with routing.
However, I have a DB Context in the original project that I'd like to use from the second. It is when I add the project reference that my issue manifests.
The issue is that I am getting ambiguous routing requests, so I need to find a way to distinguish requests, for example, to the Home Controller.
Be advised, I use attribute routing.
I've come up with a few workarounds:
Copy the DB Context into the second project. This has obvious shortcomings and is the least attractive solution, but it will work.
Set up routes with some distinguishing prefix, i.e. "/proj2Home", etc. While crude, this is the least objectionable and is straight-forward to implement. It does mandate a route value for every Url of the second project but I could hold my nose on that.
I've considered using Middleware but it looks like the error is coming from UseRouting() (although I'm not 100% on this) so I'm not sure what I can do at that point.
Does anyone know of some sort of build-in mechanism to accomplish this?

Related

ASP.NET Core Nested / Tiered Controllers

Hi there fellow Overflowing Stackers! Looking for your help in regards to having tiered or nested controllers in asp.net core
So far from what I can see, the default boilerplate code, and its default route {controller=Home}/{action=Index}/{id?} only allows for controllers to be in the default Controllers directory, which doesn't allow nesting at all.
From what I have seen thus far there are a few ways to get around this, neither which are ideal in my opinion.
Razor View Pages
Out of the box, it would appear that Razor Pages, automatically circumvent this issue as the .net core automatically attempts to traverse the corresponding folder structure from the associated URL structure to attempt to find a .cshtml file that matches the route.
The problem? This is more of an MVVM pattern than an MVC one. Which is the lesser of the 3 evils, in my opinion, (Open to debate ofc)
Route Attributes
The other alternative I see is that controllers can be associated with routes using the Route Attribute on Controllers and Actions [Route("parent/route/path")]
The problem? Same namespace (folder) means no reuse of Controller names and given the nature of our app, we need to namespace our Controllers. Not to mention that it's going to make the Controller Folder Massive, with 100's of Controller Files.
Adding More Routes
Adding more routes manually is going to be painful, as I have to add a new route for every possible permutation of parent and child within the folder structure.
Whilst I understand that this may be the intended way of doing things. Adding every possible nested folder in a new route is bad design in my opinion. As this is something that can automatically be inferred from the route to the folder structure. And is automatically done in other Languages and Frameworks automatically. (One excellent example C# Class Libraries)
Salt in the Wound
Most importantly it is apparently not possible to have the same named controller in two completely separate folders / routes:
How to use same controller name in different namespaces. When this happens you get greeted with an Exception AmbiguousActionException: Multiple actions matched.
The biggest pain point for me is that Namespaces are supported within the normal version of the .NET MVC Framework. For some wonderful reason, the namespace parameter has been removed from the MapRoute() method by someone at Microsoft. Scratching my head as to why that was done?
Not 'Just' a Rant
Before I continue, this post is not me whining or ranting, I am genuinely looking for a better solution and am looking for other ideas and alternatives. Please keep reading for my ideal solution below.
Ideal Solution
Since I have come from a PHP background I have found that simply using the default spl_register_autoloader() gave me exactly what I wanted. The capacity for me to register a specific namespace of my choosing and for PHP to look at any and all parts of the subsequent namespace in the local directory folder structure for the Controller I wanted.
i.e. the PHP controller Controllers\NestingLevel1\NestingLevel2\HomeController would look for the associated controller in Controllers\NestingLevel1\NestingLevel2\HomeController.php
Simple, Beautiful and Did I mention Simple?
Wrap Up
Is there any way I can get Nested Controllers in ASP.NET core without:
Razor View Pages (MVVM)
Route Attributes on Every Single Nested Controller
Manually Adding Routes for Every Possible Nested Parent / Child Combination

Orchard CMS - How to redirect URL request?

I've created an Orchard Website that consists of many mini-websites made operable via theme selection relating and triggered by the current [unique] URL all from the same DB.
It works as I'd hoped, but I wish to improve the Autoroute paths of my pages.
Currently I'm using:
{Content.Fields.PageOrderPart.SitesTaxonomy.Terms:0}/{Content.Slug}
Which results in:
www.site1.com/sitealpha/gallery
www.site2.com/sitebeta/gallery
What I would like is:
www.site1.com/gallery
www.site2.com/gallery
However, I still need to be able to differentiate pages with the same name [...hence why the Autoroute path above was created in the first place] - or I will obviously get permalink duplication errors.
Can anyone think of an ingenious way to sort this or is there some Orchard feature I may've missed, perhaps url rewrites or possibly an existing MVC method?
Many thanks for your input, PP
Further thoughts (hopefully this doesn’t influence any other member's sugestions):
Rewrite rules: Probably aren't dynamic enough for the amount of content I have [still increasing] - and I could see any alterations to existing permalinks being a real nightmare.
Besides, for an unkown reason - I and some other Orchard users - can't seem to get a rewrite action to work?
Routes: Honestly, I haven't played with these properly - I can see that capturing and dissecting a URL to stipulate the area / controller / action should be easy enough - but I'm not sure how to go about redirecting to a particular Orchard page?
FilterProvider, IActionFilter: I tested this scenario [which could become quite complicated code wise] and I'm not sure the performance is acceptable -- my dev system seems to really suffer with any code in the 'OnActionExecuting' method.
Update: I've investigated the IActionFilter scenario and it appears my initial performance worries were unfounded [i.e. a fresh install didn't behave any slower with some URL restructuring code on the 'OnActionExecuting' method].
My last hurdle is to discover why IIS Rewrite rules aren't working with Orchard:
https://stackoverflow.com/questions/35226580/orchard-cms-iis-rewrite-rules-do-not-work-for-rewrite-action-types
It could be that multi tenancy would be the feature you are after. You can have one code, one db but they are all completely separate sites. So separate admin areas etc.
http://docs.orchardproject.net/Documentation/Setting-up-a-multi-tenant-orchard-site

How to generate URLs to link back to objects?

I'm trying to build some RESTful services using spray. I've figured out how to build the directives I need. But the issue I'm having is how to reliable generate URLs back to the "resources" I'm working with. Note I use the term "resources" here as it is used for RESTful APIs (i.e. the server side objects one refers to through the API).
I've looked through the documentation and I haven't found any reference for this except mention of "Resources" in the Java sense (i.e. data files in the classpath).
For sure I can build a directive that maps "/items/127" to a resource on the server side. But what I don't see how to do (at least in a safe and automatic way) in Spray is how to generate such a URL given the server-side resource. I'm looking for something similar to url_for from the Flask framework.
For now, I'm writing functions to do this. But, of course, they are fragile because they aren't DRY (i.e. they don't use any knowledge of Spray routing in generating the URLS).
Am I missing something?
What you're asking for is known as reverse routing. As #iwein said, there's no direct support for reverse routing in Spray. You can confirm this from Matthias in this thread. There is an open ticket for this issue.
However, there is an approach, based on the PathMatcher that Marcel Mojzis open sourced which you can find here.
I have a need for this as well, but I'm going to get by with a "known pattern" approach until Spray (or akka-http) comes up with its own solution to this issue. Essentially, I have an object that knows how to generate the URL for certain patterns of things. Each pattern is a function and clients of the object have to ask for the url by one of the function names. Not ideal, but very simple and effective until akka-http provides a more generic solution.
I don't think that Spray has an equivalent of url_for. I don't think it would make sense in the context of Spray, because in Spray you're not annotating functions with urls that map to them, but you're creating routes that deserialize requests and eventually map them to functions.
As such there is no easy way to generate an example url from the name of a function.

Good ways to start an application in ASP.NET MVC

When you start creating an application or site in ASP.NET MVC, what do you do before typing in that first line of code?
I'm personally fond of creating a new ASP.NET MVC Web Application project and then cleaning out controllers/views until I have what is essentially a blank project (i.e. it runs but doesn't offer functionality). Then I start working on my model and adding controllers/views as needed.
I've also read about starter kits and sample applications but I have not yet started actively working with any of them. However, in my reading I have seen authors suggest that it might be good to start off with an existing template and build on it.
I'm trying to determine if there are better ways of starting off a project such that time is saved and/or the resulting deliverable is of higher quality.
The other things I do (I also clear out the controller/views etc)
Put an IOC in place.
Put ELMAH into the project.
Then I grab a coffee and write my first test.
Kindness,
Dan
PS: At some point I shall get around to creating a template for this so I don't redo it everytime. As soon as I decide upon my favourite IOC. :-)
I usually clear out the Content folder as well and put in place a nice CSS reset file and/or a CSS framework like the 960 grid
Before starting any type of project you must know what you want to do. So take a sheet of paper and start writing on here:
The name of your application
Enumerate the features
Make a quick draft of the domain model (entities that you are going to have)
Try finding the ways (choosing a technology) you are going to do different stuff like: data access, validation (client and server side), logging, IoC, Security, Caching etc.
Do a quick draft of all the views you are going to have in your application
Identify any other problems you might need to solve/implement/develop and think how are you going to do that

Nested Applications with ASP.NET MVC

I'm currently working on an asp.net-mvc content management system. It would be incredibly useful to be able to deploy nested applications e.g. /shop to have a separate app inside. Or even another instance of the cms.
I've found some information around talking about limiting the inheritance of the web.config but I've no idea how this maps to an MVC application. I'm hoping as its essentially the same stack it will just run. Nothing is ever that easy though.
If anyone has any experience doing this I would be really grateful. The idea of one big application to rule them all is not at all nice.
Si.
To be honest you biggest hurdle is going to be creating the routes and making sure they don't interfere with routes already in the system. After you get that working the rest is easy as pie.
The first thing you will need is an HttpModule that will be inserted in to the web.config under the . This module will be used to register and custom ViewEngines or Routes that you want to register. You do this in the same way that you do in the Global.asax but instead of putting them in the Application_Start you put them in the static constructor of the HttpModule. This is so they are only loaded once like Application_Start.
By do the above you will have created a module that is easily transportable and doesn't require the implimentor to modify their Global.asax code to get your stuff to work.
The second thing you probably want to do is create a custom configuration in the web.config to set stuff like the root path of your application. This will be prepended on to the route when you are setting it up in the HttpModule. Also you can use this to store customization information that is not appropriate for the database.
Last but not necessary is that you may want to create a custom ViewEngine that knowns and understands your folder structure. This is only necessary if you want to store the views in a different path than the default views, in order to minimize conflicts.
Check out the Grouping Controllers with ASP.NET MVC from Phil Haack.
Hope it helps,
Bruno Figueiredo
I've gone down this road before (with /blog), but found it to be doable but complicated and difficult to maintain. Instead I ended up using subdomains:
www.example.com
shop.example.com
blog.example.com
These are much easier to maintain because you can just have them work as separate websites in IIS. And, of course, you can always redirect www.example.com/shop to shop.example.com.

Resources