i have
routes.MapRoute("BuildingCompanyProject", "BuildingCompany/{projectId}/BuildingProject", new { controller = "BuildingProject", action = "Index", projectId = "" });
in Global.asax.cs
and is placed below the default route.
and the above route is called on clicking a link
<a title="Go toCompany" style="background: none!important" href='<%= Url.RouteUrl("BuildingCompanyProject",new {controller="BuildingProject" , action="Index" , projectId=item.Id})%>'>
beheer bedrijf</a>
But on clicking the url is mapped to default route.
How should i achieve this.
You should put custom routes above the default route, when the default route could resolve your custom route (like it is in your case).
In light of your comment, you say you have another URL "/BuildingProject" that should be resolved by the default route.
You should have then 3 routes: first the one to resolve "/BuildingProject", then your custom one that you talked about in the question, and only in the end the default route.
Move your custom route above the default route. Remember that your url will match the default route first
Related
I've seen several MVC4 tutorials that show how to access the index URL for a view, but I can't seem to reach a new view that I add.
I can access my home index at:
http://localhost:3214/
But if I create a new view (let's call it "NewView.cshtml") I can't access it from
http://localhost:3214/NewView.cshtml
Where would it be?
The page inspector expects the page to be at:
http://localhost:4244/Home/NewView
But it isn't there.
UPDATE:
In the solution explorer the file is located at:
MyProject/Views/Home/NewView.cshtml
OK - So think of your Views as merely html files (although they are not) - i.e. they are purely for display purposes. But they are not static like normal HTMLs - they have code.
Hence its the controller and the route that you need to understand.
The route is what you type in the browser. For instance /Home/NewView will be translated to HomeController, NewView action if thats how you have configured it. The default view is {controller}/{action}/{id} so try http://localhost:4244/Home/NewView/1
Now to properly display and code NewView you need to go to your HomeController and add a NewView action. Like:
public ActionResult NewView()
{
return View(); // This will automatically display the NewView.chtml view from the Home (or Shared) folder in your Views folder
}
Then go to your Routes (typically in your global.asax file and add it like:
routes.MapRoute(
"SomeUniqueRouteName",
"Home/NewView",
new { controller = "Home" action = "NewView" }
);
Then you can call it like http://localhost:4244/Home/NewView without the id cause you haev specified a route for it.
Let me know if you need any more help.
I'm building a site where I want to have only one controller, and url's that look like:
\main-page\sub-page
main-page is not the controller, and sub-page is not the action, they're just the names of pages in the database, which I want to render on the fly. In other words, there are many pages, and I wouldn't want to make a controller for each one. In fact, I want just one Home controller with one Index action that looks like this:
Function Index(ByVal Page As String, ByVal SubPage As String) As ActionResult
And there's only one view, which uses the parameters to dynamically load the page content.
The following route almost works:
routes.MapRoute(
"Default", _
"{page}/{subpage}", _
New With {.controller = "Home", .action = "Index", .page = "Landing", .subpage = "Index"}
)
But the problem is that it's also picking up requests for files, e.g., /images/somefile.jpg, because that matches the route.
So the question is: how can I do this without also incorrectly getting the image requests OR is there a totally different way to do the route to support what I'm trying to do?
Could you maybe try to ignore the route i.e.
routes.IgnoreRoute("{*alljpg}", new {alljpg=#".*\.jpg(/.*)?"});
This is based on this article which is probably more helpful than this brief post.
Well it's not a complete guess to be honest. I had a related problem with the favicon causing additional requests. The below code solved it so the principal is sound
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });
I have two secure sites:
siteA.example.com
siteB.example.com
A common sign in page exists on siteA.example.com. If you visit siteB.example.com without logging in, you are redirected to siteA.example.com. The URL ends up looking like this:
http://siteA.example.com/Account/LogOn?ReturnUrl=%2f
This doesn't work for me, because the return URL actually just ends up taking you to the homepage of siteA.example.com even though the user visited siteB.example.com.
Is there a configurable way to fix this? Or should I just hook into one of the global life cycle events to change the response?
You could write a custom module or use a custom authorize attribute.
I found this little trick :
change your config for
<forms loginUrl="http://google.com/Login?domain=http://google.fr/" />
Where google.com is where you'll enter your credentials and google.fr is the app needing a logged in user.
Then you'll just have to concatene this with the parameter ReturnUrl before redirecting the user.
When setting the value for returnURL use UrlHelper. Use the overload that allows you to set the host name, and pass in sizeA.example.com.
UrlHelper.GenerateUrl(null, "action", "controller", "http", "siteA.example.com", null, new { id = "Awesome" }, routes);
I am having problem invoking my Log-on action method, which is in a controller outside current Area.
I get the following error message :
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.
Is any way of calling the root controller that is outside areas in the main controller folder ?
If you are in an area, you'll need to specify a blank one to access your root controllers.
#Html.ActionLink("Back to Home Controller", "Index", "Home", new { area = "" }, null)
You can pass Area in route parameters to ActionLink and other methods.
I know this is probably straight forward, but this isnt working for me, I want to simply connect this to trigger a viewController when I click it. When I create a controller and a view and click it it throws a 404 and the code never hits the controller.
<div> All Accounts In the System</div>
You should probably follow few tutorials for mvc. Links are by default using System.Web.Routing magic (if you have default template, look into global.asax). Default url looks like /controller/action/id where id is optional, action defaulted to Index and controller defaulted to Home. For link construction in views use these helper methods:
#Html.ActionLink("All accounts in the system", "AdminReports", "Admin");
or
All accounts in the system
The link should look like /Admin/AdminReports with default settings.
You want something like this:
<a href='<%= Url.Action("Controller", "Action") %>'>Text</a>
where
Controller = Your controller name.
Action = Your action method.