Let's say the domain that is mapped to my root hosting directory is example.com. GoDaddy forces mapping of other domains to subdirectories of the root. For example, my second domain, example1.com, is mapped to example.com/example1.
I uploaded my ASP.NET MVC site to such a subdirectory, only to find that ActionLinks that are for navigation have the following format:
http://example1.com/example1/Controller/Action
In other words, even when I use the domain that is mapped to the subdirectory, the subdirectory is still used in the URL. I want to change the format of my ActionLinks.
However, I noticed that I can also access the same path by going to:
http://example1.com/Controller/Action
(leaving out the subdirectory)
I want to have my ActionLinks automatically drop the subdirectory, as it is not required.
Is this possible without changing the ActionLinks into plain-old URLs?
No, I don't think so, as an action link mainly works to render the controller/action. The other work around, if you have install access for the server, is to use an URL Rewriting tool like iirf.codeplex.com, which is free and works pretty good. There may be some other unintended consequences with rewriting though, depending on what you are doing.
HTH.
You could try adding additional route statements in your global.asax, in order from your subdirectories. Example:
routes.AddRoute("example1/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
routes.AddRoute("{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
I believe the routes are checked in order from the global asax, so you could effectively route the request to the right spot. However, your link would still contain the 'example1' folder in the URL.
Related
I'm going through the exercise from the book (just starting to learn MVC). I have made the following change to RouteConfig.cs file:
routes.MapRoute("Contact", "Contact/{*pathinfo}", new {
controller = "Home", action = "Contact" });
routes.MapRoute("About", "About/{*pathinfo}", new { controller =
"About", action = "About" });
The contact page works absolutely fine but the About just throws error when trying to access it via http://localhost:49899/About
Error: Server Error in '/' Application. The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its
dependencies) could have been removed, had its name changed, or is
temporarily unavailable. Please review the following URL and make
sure that it is spelled correctly. Requested URL: /About
I cannot see any difference between those 2 lines of the code, can anybody point me to what I'm missing? This is a brand new project with all the default settings and scaffolding.
When you created the default project, all kinds of wizardry happened behind the scenes for you. The default project most likely came with an auto-generated HomeController, an auto-generated Home folder to contain Views that the Home Controller will call, some Actions that correspond to those Views.
You probably do not have a Controller named About. The route that you have defined for About is saying that to get to the About page you would go through the About controller and call the About Action. Most likely you need to hit the Home Controller and then the About Action.
P.S. MVC uses naming conventions to infer where things are. You'll probably find an About view in the view folder named Home. Are you seeing a pattern here?
Routing docs
I moved a solution from one machine to another and am having nightmares. I've got one problem left. The default page can't get served, it says Resource cannot be found. I have in the routeConfig.cs
routes.MapRoute("Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
I get Resource cannot be found.
I can access the default page by :-
http://example.com/home/index
The other questions don't seem to answer it.
EDIT: If I put the above line of code at the top of the Routing code, the default page works but other pages don't. If I put the code at the bottom, other pages work but the default page doesn't.
Try look in properties there is "Web" tab were you cat chose server and project URL, hope
If you have only default routing on RouteConfig file in development machine and keep getting 404, it seems that your problem comes from project's virtual path settings.
http://forums.asp.net/t/1893154.aspx?The+resource+cannot+be+found+404
If application is deployed on local host (or you have set Use local IIS
Web Server) then it means you have created a virtual directory named
"YourSite".
Right click your app project on VS, choose Properties, then go to "Web" part and set your site's virtual path on "Project Url".
For virtual directory problem on live IIS server, see here to add virtual directory into destination machine before deploying your app.
In addition, if you have custom routing rule, make sure the custom route placed properly on top of default routing rule.
The problem was that I had two Config Rules pointing to the same address. One of the other rules was pointing to the wrong controller. The wrong controller was erroring because I had set up the action, I had used the name of the controller as the action, thats a no-no. The above rule was correct but the incorrect rule was
routes.MapRoute(
name: "pagelist",
url: "pagelist",
defaults: new { controller = "pagelist", action = "pagelist", id = UrlParameter.Optional }
);
the action should have been Index.
I am using a MVC Web Api server application for generating client data. In addition, I want to publish a collection of JavaScript, XAP (Silverlight) and XML files to my client-side application. Currently, I have a project structure in which those directories are mixed through my .Net implementation code (what I do not like), as the client app uses URIs to request these files can not be changed. Nevertheless, I want to separate the client data from the server application implementation in a different folder.
Therefore, is there a way to store the client data in a separate folder, e.g.
/clientdata/JavaScript
/clientdata/XAP
/clientdata/XML
/clientdata/...
in my project, while still being able to access these files using URIs like
/JavaScript
/XAP/
/XML/
Which are the URLs used by the client app and again can not be easily changed.
This is the main method of my Global.asax
AreaRegistration.RegisterAllAreas();
RouteTable.Routes.MapRoute(
name: "Default",
url: string.Empty,
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "TCMsimulator.Controllers" }
);
var config = new Microsoft.ApplicationServer.Http.HttpConfiguration();
RouteTable.Routes.MapServiceRoute<ResourcesService>("resources/", config);
RouteTable.Routes.MapServiceRoute<PublicResourcesService>("publicresources/",config);
RouteTable.Routes.MapServiceRoute<MonitorService>("monitor/", config);
Is there a way to add a folder redirect to the routing table, such that a virtual folder in the URI is redirected to a filesystem folder? Like can be done using ModRewrite in Apache? It seems like a simple problem, but I have not found a solution to it in Web Api.
Thanks in advance!
I recommend using Attribute Routing. It is much more understandable. You can see examples of multiple routes mapped to a single resource.
http://attributerouting.net/
If these directories only contain files and not Web Api generated output, you should probably ignore the routes to the directories and make sure the requests for them run outside of Web Api, like regular static files that are served from IIS (e.g. images).
I have created a folder admin inside controller folder and wrote few controllers in it. I want to access all functions in that controllers in that folder by a url like abc.com/admin//.
I can get it working directly when its put directly in controller folder.
EDIT
WHAT HAVE YOU TRIED?
Just created a folder and wrote normal controllers in it. But I don't know how to route it.
Well, i would suggest, rather than just creating a folder and create controllers inside of it. You should create an Area
When you create an Area in asp.net mvc, it will automatically create folders(controller, model etc) for you. And the routing setup as well.
For more info, please visit
http://www.codeguru.com/csharp/.net/net_asp/mvc/article.php/c20227/Using-Areas-in-ASPNET-MVC-Application.htm
Add a route for the admin controllers in your route config if you want to support that path in the URL. Something like:
routes.MapRoute(
name: "Admin",
url: "admin/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But #DotNetDreamer is right that Areas are a better solution for the admin functionality of your site.
Sometimes when I launch my MVC 3 project it attempts to load the fully qualified URL for the view being rendered instead of the action within the controller (Which gives me a 404 error). Other times it works fine and actually hits the controller action like it's supposed to, but it's about 50/50.
The URL it hits sometimes is: http://localhost:xxxx/Views/Account/LogOn.cshtml
Here is the default route setup in the Global.asax file:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional }
);
I also tried removing the /{id} parameter from the route as I don't feel it's needed for the logon screen.
Any ideas? Currently the project is setup pretty simply with the default action method LogOn in the AccountController etc. The only thing I did was change the controller and action in the global.asax file.
Try this :go to Project Properties > Web > Start Action
And check the Specific Page option - leaving the text box blank.
You are probably using Visual Studio and you probably are actively editing a .cshtml page when you hit debug.
Try launching the debugger when you are either looking at a code file or a file from a project that isn't in the startup project (ie, your EF/model project) and see if that launches the debugger to the correct URL.
There might be a setting in the project properties that specifies the startup URL. I'll look for it and edit this post if I find it.
I'm guessing you using cassini (builtin dev web server in VS.Net)? If so I get this all the time and seams to be a bug in VS.Net. Switch to IIS 7.5 and you don't get it any more