Log-on doesn't work from other Areas - asp.net

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.

Related

Xamarin Forms Shell navigate back with GoToAsync("..") causes exception

I have a very simple navigation stack consisting of a root page and then a model page on top of it. My root page is being set as the root by being the first page to be registered with Shell.
public AppShell()
{
InitializeComponent();
// opening view
Regester<LoginPage>();
Regester<SignUpPage>();
...
}
private void Regester<T>()
{
System.Diagnostics.Debug.WriteLine($"Regestering with shell : {typeof(T).Name} - {typeof(T)}");
Items.Add(new ShellContent { Route = typeof(T).Name, ContentTemplate = new DataTemplate(typeof(T)) });
Routing.RegisterRoute(typeof(T).Name, typeof(T));
}
}
Then I am navigating to the model sign up page using relative routing
Shell.Current.GoToAsync("SignUpPage");
or I will use absolute routing
Shell.Current.GoToAsync("///LogInPage/SignUpPage");
then I will attempt to navigate back in the stack using
Shell.Current.GoToAsync("..");
But I get this exception
Global routes currently cannot be the only page on the stack, so absolute routing to global routes is not supported. For now, just navigate to: LoginPage/
At the time of exception the CurrentState.Location property is this
Location = {///LoginPage/SignUpPage}
I don't understand why this is happening. This will also happen if I am further into the stack and doing something like trying to navigate back from a detail page. What do I need to do to be able to use GoToAsync("..") properly?
Cause:
Your path ///LogInPage is invalid . Global routes currently can't be the only page on the navigation stack. Therefore, absolute routing to global routes is unsupported.
Solution:
Navigation can also be performed by specifying a valid relative URI as an argument to the GoToAsync method. The routing system will attempt to match the URI to a ShellContent object. Therefore, if all the routes in an application are unique, navigation can be performed by only specifying the unique route name as a relative URI:
await Shell.Current.GoToAsync("SignUpPage");

URL for new views in MVC4

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.

How to correctly link between different Views

I have a master.cshtml with a navigation bar. My first link is to another view, ProjectManagement
<li>Project Management</li>
master.cshtml is in /Views/Shared/_master.cshtml
ProjectManagement is in /Views/ProjectManagement.cshtml
Whenever I click on the link I get:
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: /Views/ProjectManagement.cshtml
Am I using the wrong path or should I try to access the page differently?
Edit: I was able to get close by using this:
<a href="#Html.Action( "ProjectMgmt", "Service", "Project Management" )">
The only problem is that it now puts the page into my navigation bar! I only want it to link to page, what could I be doing this time?
You don't link to directly to views, you link to actions. Actions are implemented as methods on a controller; these methods are located using the routing system.
Example Controller:
public class ServicesController : Controller
{
[HttpGet]
public ActionResult ProjectManagement()
{
// automatically locates the correct view; you can also explicitly
// pass the path to the view
return View();
}
}
You can now right-click on the action method name ("ProjectManagement") and select "Add View". This will help you create a new view, and put it in a location which can be automatically found by the view engine.
Views are typically placed in a "Views/[ControllerName]/" folder, e.g. "Views/Services/ProjectManagement.cshtml".
To link to this action method in your navigation bar, you can use the helper method ActionLink().
<li>#Html.ActionLink( "Project Management", "ProjectManagement", "Services" )</li>
See also: Controllers and Routing

My route includes image file requests when I don't specify a controller

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(/.*)?" });

problem in routing in asp.net mvc

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

Resources