Im currently implementing MVC into a fairly large project and want to sort views into categories. MVC dont seem to understand this and Im having problems finding a clean solution. Basically I was hoping to solve this with routes, but it isnt working.
My folder structure in the project is like this:
- Controller
- SLResources
- FAQController.cs
...
- View
- SLResources
- FAQ
- (cshtml files in here)
Ive also tried adding a - FAQ folder after - SLResources in the controller folder structure.
Ive then made the following routing, with no luck:
RouteTable.Routes.MapRoute(
name: "FAQ",
url: "SLResources/FAQ/{action}/{id}",
defaults: new { controller = "FAQ", action = "Index", id = UrlParameter.Optional }
);
basically I would like to reach the FAQ by using this url: http://www.xxxxxxxx.com/SLResources/FAQ/
Is the only solutions to either create a dummy class that redirects to the proper views, or a custom ViewEngine?
Any tips?
If your goal is to have the url with SLResources/FAQ, then you don't need to create a separate folder in the Views.
Your route already does that for you. Leave your route as is, and put your cshtml files in the 'Views>FAQ' folder and it will work.
Related
I'm using ASP.NET Core MVC and using attribute routing in my controllers.
In my Configure method in Startup.cs I currently call app.UseMvc() to start my MVC application.
Everything works as expected.
Now I'm trying to get a catchall going, but the 404 always gets the best of the situation.
I changed my app.UseMvc to the following:
app.UseMvc(routes =>
{
routes.MapRoute("Default",
"{*catchall}",
new { controller = "Index", action = "Index" },
new { catchall = #"^(.*)?$" });
});
but no dice.
I even tried to remove the catchall regular express as well, but I still get forwarded to my 404 page.
Any help is appreciated!
For anyone having the same problem, Tom Droste pushed me into the right direction
Adding a conventional route and using attribute routing has subtle side effects
The catch-all route was added first in the route dictionary and the attribute routes are added afterwards.
The AttributeRouting class just creates an instance of AttributeRoute which is an IRouter responsible for building the attribute routes. This is done on the first time AttributeRoute is invoked
Having that, the catchall was thus never called because it's not the last route.
putting everything back into conventional routing fixed the problem.
I suggest reading https://luisfsgoncalves.wordpress.com/2015/08/18/asp-net-5-routing-part-ii/ to understand how attribute routing works
Have you tried this?
routes.MapRoute(
"Default",
"{*.}",
new { controller = "Home", action = "Index" }
);
In your Startup.cs file, place this line of code just above app.UseMvc() in the Configure() Method. Obviously you can redirect to whatever page you want.
app.UseStatusCodePagesWithRedirects("~/Home/Index");
EDIT:
Per you comment, as far as I know, you'd have to write your own middleware to redirect and keep the original url/query parameters. If you're not familiar with developing middleware, I posted a demo project on GitHub that demonstrates how to develop middleware in three easy steps. You can download the project here.
So if I understand you correctly you just want a route that will be the one when the rest isn't matched. In my knowledge there are atm two ways to fix this. The correct one would be to write some routing middleware, however the dirty solution is to create a route with all nullable arguments that will be matched as last.
routes.MapRoute(
"DirtyRoutingSolution",
"{notUsed?}/{notUsedTwo?}/{notUsedThree?}/{notUsedFour?}",
new { controller = "Home", action = "Index"}
);
I have a project I have attached Piranha CMS too. That bit worked fine.... However the pages I have already created and the template are not displaying in the CMS edit.
Steps I have done.
imported Piranha from guget.
Altered webconfig to passive.
removed piranha connection section.
changed connection to forms.
Alter connection string to match sql-server DB.
altered the global.config
built DB in sql server
Built project setup admin and the DB structure built followed.
Like I said it has built ok... But only the standard template. I'm guessing I've ether missed a step or there are steps to go. Can you advise me were to go next?
The webpage site currently just a home page.. The remaining pages are not completed. But if I don't crack this point there is little point continuing.
I think you have to add the pages into Piranha through the manager interface, then use the Permalink as the argument to fetch the Piranha managed content from Piranha. I don't think Piranha will parse your view/controller/routes and automatically import them into the site definition.
I use Piranha in my views like this;
#Html.ActionLink((string)Model.CMSData.Properties.WebSiteTitle, "Index", "Home", new { area = "" }, new { #class = "navbar-brand" })
I embed the CMS data into my model based on the examples in the Getting Started section;
public object CMSData = Piranha.Models.PageModel.GetByPermalink("my-permalink") ;
This enables me to weave in CMS content to my Model and then use it inside my View.
I am trying to create two MVC applications, and in first application created one view and compiled the application, and in the second application i have added the dll of the first application.
in the routeconfig.cs file added the below line.
"Structure of First application"
Application1
Controller
Home(Actionmethod is Home)
View
Home.cshtml
Application2: references:Application1
_layout.cshtml.
trying to access url: localhost/Home
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "TestApp.Controllers" }
and i am trying to call the view from the first application, but it is not able to find the view from the first application, can any body tell me any other configurations do i need to add to access the view of first application.
Thanks in advance.
Please explain why you have them in two separate mvc applications.
app 1.
controller
view
model
app 2.
controller
view
model
Im not sure if view are compiled into the .dll if they are. Then how are you accessing them?
Also you need to register your search locations for your views... I assume your are using the razor engine.
http://theshravan.net/blog/configure-the-views-search-locations-in-asp-net-mvc/
Can I specify a custom location to "search for views" in ASP.NET MVC?
Basically you need to register the search locations for your views the default is
App
Views
[ViewName]Controller
I have a strong feeling the app which references the .dll [App1]
Has nothing in it to tell it to look in App2 for views as well.
Hope this makes sense ;-)
Try creating Area in your project and create required controller and views instead of accessing view of another project.
Is there a way using routes in mvc to shorten the url? I'm fairly new to MVC, and I guess I don't quite yet understand what routes.IgnoreRoute()does fully.
So for the sake of example, using routes could I make this,
site.com/webFolder/controller/action
look like,
site.com/controller?
Also if it helps we're currently using IIS7. Thx all.
Yes you can create custom routes. Take a look at this:
http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/creating-custom-routes-cs
In the example given a route of /Archive/{entryDate} was created using
routes.MapRoute(
"Blog", // Route name
"Archive/{entryDate}", // URL with parameters
new { controller = "Archive", action = "Entry" } // Parameter defaults
);
I've managed to create several forms using YII and GII, but I'll be honest, I don't understand it perfectly. If anyone can explain this to me, I'd really appreciate it.
Basically, I create accessed the form creator here:
index.php/gii/form
I then filled in:
Model Class: Product
View Name: createProduct
View Path: application.views (default)
Scenario: createProduct
When I generate the code, I end up with a file called "createProduct.php" located at:
protected\views\createProduct.php
How do I access this view? I figured something like this should work: http://mysite.com/createProduct (but it doesn't)
What do I do if I want to change the link to be: http://mysite.com/admin/products/createProduct
Any help would be greatly appreciated!
Gii tells you to modify your controller. It shows the code you should insert after generating process. When you do this you'll be able to access your form by http://yoursite.com/controller/action
You can manage your urls using UrlManager. Look this article in docs - http://www.yiiframework.com/doc/guide/1.1/en/topics.url