asp.net mvc area Routes stopped Working - asp.net-mvc

I have had a working project for months now....This week I decided to change some of my areas....I deleted two areas and created a new Area...
After doing this I can't add any new controllers to the existing areas and get them to route correctly. I am seeing other strange things....for instance I am not getting autocompletion of the services path....It only list the controllers within the areas and not the models or the services....
I have created new controllers in the new areas...and these worked fine.....but something is messed up in the "old" areas....
I am not sure where to start looking for this.....in some ways it sounds like a routing issue...but the routes did not change....so I think it might be a configuration issue.... but which configuration file....each area has its own configuration file and then we have the master configuration file....
Has anyone see a problem like this?

You can use RouteDebugger to debug your routes.

I found a partial answer....At the same time that I changed the areas I renamed the project.... When I created the new areas they got the namespace created using the OLD project name....and I did not notice the namespace was wrong.... The clue to this was the fact that path completions were not working....I should have looked more closely at this....
Now the big question, and the reason that I am marking this as the answer is....where does the project name get retained in asp.net mvc..... I have looked in the web.config and I have done a global search for the old project name ..... Any clues where to change this???

Related

MVC Tag helpers not working

I have an issue with tag helpers.
All I want to do is create a form that posts data to the controller - basic enough I thought but it doesnt work in my project.
I create a brand new asp.net core web application with default setup and it works there but my project refuses to recognise the tags and act accordingly.
Notably the markup doesnt highlight the same or provide info when hovered over so some ref or something fundamental is missing, can anyone advise.
*Default project working with correct highlights
*My project - not highlighting the code right or working.
Project.json is the exact same so I have no idea whats missing.
FOUND IT!
Microsoft like to change things. So, MVC 6 (asp.net core ...etc) can make use of a _ViewImports.cshtml file in the views folder (feel free to add it yourself if you like me made a project before this existed).
This acts like global import file, and to make the tag helpers available in all your views, you need to add reference here.
Mine now looks like this:
#using Mobile.Models
#addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
And boom - tags look correct and its work as expected.
You will also face this issue when you create a new Area in the asp.net core MVC project, to resolve it copy ~/Views/_ViewImports.cshtml to ~/AreaName/Views/_ViewImports.cshtml
Make sure that you MUST specify the assembly name and not the namespace.
That was my mistake.

Code generation: Custom controller templates

To start off, I'm using Asp.Net MVC 4, and I've modified my solution to contain the CodeTemplates folder by following this tutorial. (I think my issue might be MVC related?)
I've replaced the code in the Ajax grid template with a with my own JQgrid-template, which was fine for getting my project started. But I'm having trouble creating completely new controller templates (well the having them show up in the create new controller menu)
any ideas?
Update
I've taken a few stabs at using the scaffolding feature, but it's not exactly what i had in mind (although it does work like expected. I watched a video by steve sanderson on the topic that was quite informative), any way to get my custom views/controllers inside the add controller gui?
As far as my understanding of your problem goes, you cannot manage two controllers within one .ps1 file, which corresponds to your "AddController" directory.
You have to use the Package Manager and issue a command like
Scaffold CustomScaffolder <choose-name>
This will generate another subfolder below CodeTemplates with the name . From there, you open and edit t4 files like you did with your existing one.
More info here

ASP.Net MVC 2 Areas: The partial view '...' was not found

We recently upgraded a project to MVC 2 and we'd like to use Areas but there is an issue.
We have created a new area, setup a controller, configured a route, and created a view in the correct location. When we run the code it successfully finds the route and hits the controller but when it goes to render the view there is an exception.
The web forms view engine doesn't seem to be looking in the Areas section for views. The error we're seeing is:
~/Views/<ControllerName>/<ViewName>.aspx
~/Views/<ControllerName>/<ViewName>.ascx
~/Views/Shared/<ViewName>.aspx
~/Views/Shared/<ViewName>.ascx
When it should be:
~/<AreaName>/Views/<ControllerName>/<ViewName>.aspx
~/<AreaName>/Views/<ControllerName>/<ViewName>.ascx
~/<AreaName>/Views/Shared/<ViewName>.aspx
~/<AreaName>/Views/Shared/<ViewName>.ascx
~/Views/<ControllerName>/<ViewName>.aspx
~/Views/<ControllerName>/<ViewName>.ascx
~/Views/Shared/<ViewName>.aspx
~/Views/Shared/<ViewName>.ascx
This would indicate that it's still somehow using the MVC 1 dll but we've looked carefully and can find only references to the V2 of MVC (there was a V1 reference in xVal, a third party DLL, but fixing that didn't make a difference).
I can only imagine that we missed something when we did the upgrade or that we've got some unusual edge case because there doesn't seem to be anything on the web that matches the problem we're experiencing.
What things could we look at that might help us resolve this issue?
Thanks in advance for any help provided.
Cheers,
Zac
What things could we look at that might help us resolve this issue?
Make sure Area Routes are Registered AreaRegistration.RegisterAllAreas(); are registered first. Area routes need to be registered as well.
Make sure generated URL links have the area name included as one of the arguments when using ActionLink and other related URL helpers
Sometimes just delete and re-add the Area from scratch. Sounds silly, but it works.
The related SO question on the left hand side of this page may help. I have linked one I think may be of value

Changing ASP.NET MVC default folder structure

Is it possible to change around the folder organization to the following and still have MVC work.
P1
Controllers
Models
Views
P2
Controllers
Models
Views
etc..
This looks a lot like the new feature area's which is available in MVC 2 though it is currently in preview and will be release with VS 2010.
If you want to do it yourself without using areas then check out this article on view engines
as said before, the Areas feature is what you are looking for.
If you are using vs2010, then although you have MVC2, you don't have the tools for creating the areas - these are only available on vs 2008, when you install the MVC2 rc
to make it happen follow the instructions at the following link: http://msdn.microsoft.com/en-us/library/ee671793%28VS.100%29.aspx
as you can see the menu options are missing so:
download the example project from there.
build the directory structure at your solution according to the example
change the AreaRegistration.cs to match your area name
add AreaRegistration.RegisterAllAreas(); at the RegisterRoutes function at the global.asax, right after the IgnoreRoute
NOTE: the namespaces shuold contain .Areas.
for More explanations search at stackoverflow for asp-net-mvc-2-beta-single-project-area-registration-getting-http-404 (since this is the first time i post an answer - SO doesn't let me append more then 1 link, go fugure..)
Hope this helps
Should be doable, I think the default folder structure is just a suggestion. I've seen before in some documentation that larger projects would likely be split up differently, possibly even among multiple assemblies.
The only thing I would think would cause some trouble would be the views. Since controller actions are mapped to them only by file name. I know there's some way to change them, but I have not had to do so yet.
You can create your own viewengine to solve the paths. That sounds like a very strange idea, but the ViewEngine class is actually responsible for 2 things: locating a view and rendering a view to the httpresponse. The thing you have to change is the locating part, you can just inherit the default aspxviewengine and change the view locating part.

Adding Asp.net mvc to existing web app how to get scaffold options

I followed the guidance in the Professional Asp.net 1.0 Wrox book for adding the MVC references to an exisiting web application and it works well except for the scaffolding options. When i right click a controller i do not get the scaffold view options that you get in a new asp.net mvc app. I am sure there is a .csproj hack that is needed to get the scaffold options but i can't find any references anywhere. Has anyone else run into this and found a solution?
Well google to the rescue. I found an answer at the following blog: http://wildermuth.com/Tag/ASP.NET+MVC
You need to edit the .csproj file and add to ProjectTypeGuids {603c0e0b-db56-11dc-be95-000d561079b0}. Order seems to matter. Originally i added to the end of the guid list and the project would not load and threw an unsupported error. I created an empty mvc site and looked at the guids and the guid above was listed 1st. So i added it to the beginning and everything worked fine.
Have you looked at how you register the data context in you Global.asax file?
This link may have more details that can help:
http://msdn.microsoft.com/en-us/library/system.web.dynamicdata.metamodel.registercontext.aspx
and this:
http://msdn.microsoft.com/en-us/library/system.web.dynamicdata.metamodel.aspx

Resources