Asked by:
linking Routes that are in different folders within Areas

Question
-
User-345777704 posted
Hello everyone ,im new to Asp mvc , i have a web application for a point of sale software and i am trying to link the pages. Coming from Home to another page works ,but getting from that page back to Home isn't working. Even when i try to to move from say a page for Purchasing to a Page for Sales ,it gives a resource not found error 404 .When you try to move from one page to another within Areas ,it assumes you're moving from within that particular directory for example ,coming from Purchasing to Sales ,the url that comes out is Purchasing/Sales ,yet it should just be Sales/..
Help.
Wednesday, June 12, 2019 8:32 AM
All replies
-
User753101303 posted
Hi,
You are using ActionLink in MVC 5 ? See https://riccardo-moschetti.org/2014/08/05/url-routing-in-microsoft-mvc-5-when-areas-are-present-potential-pitfalls/
If ASP.NET Core the doc at https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/areas?view=aspnetcore-2.2 is quite good.
Wednesday, June 12, 2019 8:52 AM -
User1520731567 posted
Hi Kundai,
According to 404 error,it means compiler can't find the right url,
your url didn't match right Area,right controller,or right acrion...
I suggest you could pay attention to RouteConfig.cs and XXXAreaRegistration.cs in Area, they configure routing.
And if you redirect to different areas,you need to specify the area,like:
@Html.ActionLink("Go to Sales Home","Index", "Home", new{area="Sales"}, null)
return RedirectToAction("Index", "Home", new { area = "Sales" });
In addition, if there are multiple “Home” controllers in the same project,you could use the namespaces attribute in the default route in “RouteConfig.cs” :
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapMvcAttributeRoutes(); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new string[] { "namespacesName.Controllers" } ); }
If you still have problems,you could post your code,so that I can reproduce your issue.
More details about Area routing,you could refer to:
https://www.c-sharpcorner.com/uploadfile/8ef97c/what-is-areas-in-asp-net-mvc-part-6/
Best Regards.
Yuki Tao
Thursday, June 13, 2019 3:28 AM -
User-345777704 posted
hello hie thats a great post but im using version 4 of mvc ,how do i import the package using System.Web.Mvc; to support areas
Thursday, June 13, 2019 11:13 AM -
User753101303 posted
It should be very similar if not identicial to MVC 5 ? Which problem do you have when you try ?
Thursday, June 13, 2019 11:17 AM -
User-345777704 posted
can we use team viewer if you have time so that you can assist me by seeing the code and errors that show.
Friday, June 14, 2019 7:22 AM -
User1520731567 posted
Hi Kundai,
hello hie thats a great post but im using version 4 of mvc ,how do i import the package using System.Web.Mvc; to support areasDidn't you apply Area correctly before?
If you want to use Area in MVC4,you could refer to this article:
https://www.codeproject.com/Articles/714356/Areas-in-ASP-NET-MVC
can we use team viewer if you have time so that you can assist me by seeing the code and errors that show.You could share more details about your code and how config in your Area,so that I can reproduce your issue and understand clearly.
Best Regards.
Yuki Tao
Friday, June 14, 2019 9:15 AM -
User-345777704 posted
// the code in the Global.asax file
namespace kendoPOS
{
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");// AreaRegistration.RegisterAllAreas();
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional } );routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }// namespaces: new[] {"kendoPOS.Controllers"}
);}protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}//code for part of purchasing controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using kendoPOS.Areas.Purchasing.ViewModels;
using PosDAL;
using System.Collections;
using kendoPOS.Models;
using System.Data.Objects;
using kendoPOS.Helpers;
using PosBL.Components;namespace kendoPOS.Areas.Purchasing.Controllers
{public class PurchaseOrdersController : Controller
{
private POSEntities db = new POSEntities();public static List<vPurchaseOrderDetail> vPurchaseOrderDetailsList = new List<vPurchaseOrderDetail>();
public static PurchaseHeaderModel tempPurchaseOrderHeader = new PurchaseHeaderModel();
//
// GET: /Purchasing/PurchaseOrders/public ActionResult Index()
{
var purchaseOrderHeaders = db.vPurchaseOrderHeaders.ToList();return View(purchaseOrderHeaders);
}Home page url is http://localhost:2276/
from home to PurchaseOrders :
http://localhost:2276/Purchasing/PurchaseOrders
from PurchaseOrders to SalesOrders:
http://localhost:2276/Purchasing/Sales/SalesOrders ,it assumes SalesOrders is in Purchases ..
Friday, June 14, 2019 9:55 AM -
User753101303 posted
You still don't show you create this wrong link. What if you try :
@Html.ActionLink("Go to Sales","SalesOrders", "Sales", new{area=""}, null)
Edit: and if you have an error just always show the error message you have and on which line it happens without waiting for someone to ask. For now my guess is that you are just building yourself a href="Sales/SalesOrders"/ link which is relative to your current location.
Friday, June 14, 2019 10:50 AM -
User-345777704 posted
Hi ,first the code for my _layout.cshtml
<!DOCTYPE html>
@*background-image:url('Images/bg_main.jpg') radial-gradient(circle at center center , rgba(248, 251, 255, 0.05) 0%, rgba(89, 147, 255, 0.05));background-size:100% auto;background- top;background-repeat:no-repeat;*@
<html>
<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/kendo/2013.2.918/kendo.common.min.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/kendo/2013.2.918/kendo.dataviz.min.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/kendo/2013.2.918/kendo.default.min.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/kendo/2013.2.918/kendo.dataviz.default.min.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/kendo/2013.2.918/jquery.min.js")"></script>
<script src="@Url.Content("~/Scripts/kendo/2013.2.918/kendo.all.min.js")"></script>
<script src="@Url.Content("~/Scripts/kendo/2013.2.918/kendo.aspnetmvc.min.js")"></script>
<link href="~/Content/kendo/2013.2.918/kendo.default.min.css" rel="stylesheet" />
<link href="~/Content/PosStyle.css" rel="stylesheet" />
<script src="@Url.Content("~/Scripts/kendo.modernizr.custom.js")"></script>
</head>
<body >
<div id="header">
<div class="float-left">
<p class="site-title">@Html.ActionLink("InotoPOS", "Index", "Home")</p>
</div>
<div class="float-right">
<section id="login">
@Html.Partial("_LoginPartial")
</section>
</div>
</div>
<div id="MenuContainer">
@Html.Partial("_Menu")
</div>//from partial i have the _Menu.cshtml code as follows:
@{
ViewBag.Title = "_Menu";
}
@(Html.Kendo().Menu()
.Name("Menu")
.Items(items =>
{
items.Add().Text("Home").Action("Index","Home");
items.Add()
.Text("Administration")
.Items(children =>
{
children.Add().Text("Setup")
.Items(innerChildren =>
{
innerChildren.Add().Text("General")
.Items(innerInnerChildren =>
{
innerInnerChildren.Add().Text("Users");
innerInnerChildren.Add().Text("Security");
});
innerChildren.Add().Text("Financials")
.Items(innerInnerChildren =>
{
innerInnerChildren.Add().Text("Period Setup").Action("index" ,"Financials/AccPeriods");
innerInnerChildren.Add().Text("Account Batch Setup").Action("index", "Financials/AccBatches");
innerInnerChildren.Add().Text("G/L Account Determination").Action("index", "Financials/GLAccountDeterminations");
innerInnerChildren.Add().Text("Item Group Account Determination").Action("index", "Financials/ItemGroupAccountsSetup");
});
innerChildren.Add().Text("Purchasing")
.Items(innerInnerChildren =>
{
innerInnerChildren.Add().Text("Users");
innerInnerChildren.Add().Text("Security");
});
innerChildren.Add().Text("Sales")
.Items(innerInnerChildren =>
{
innerInnerChildren.Add().Text("Users");
innerInnerChildren.Add().Text("Security");
});i am using Action method
Friday, June 14, 2019 11:14 AM -
User1520731567 posted
Hi Kundai,
According to your code,I find your PurchaseOrdersController in Purchasing Area.
But I can't see any Area route config in your code.
If you did not change your RegisterArea function,it should be:(based on default set)
public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Purchasing _default", "Purchasing/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); }
so,from home to PurchaseOrders:
@Html.ActionLink("Go to PurchaseOrders","Index", "PurchaseOrders", new{area="Purchasing"}, null)
from PurchaseOrders to SalesOrders:(if SalesOrderscontroller in Area named SalesOrders)
@Html.ActionLink("Go to SalesOrders","Index", "SalesOrders", new{area="SalesOrders"}, null)
Best Regards.
Yuki Tao
Tuesday, July 2, 2019 7:50 AM -
User-345777704 posted
thank you..it worked
Monday, July 8, 2019 11:54 AM