我有一个LocationAreaRegistration.cs
申请。在该应用程序中,我有一些区域,其URL未正确解析。 context.MapRoute(
"Location_default",
"{culture}/{controller}/{action}/{id}",
new { culture = "en", action = "LocationIndex", id = UrlParameter.Optional },
new { controller = "(LocationIndex)" }
);
如下:
route.config
我的routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { culture = "en", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Locator.Areas.Location.Controllers" }
).DataTokens.Add("area", "Location");
如下:
route.config
我还尝试更改routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { culture = "en", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Locator.Areas.Location.Controllers" }
);
,如下所示:
The resource cannot be found
没有一种方法有效,我发现LocationAreaRegistration.cs
错误。
但是当我按如下方式更改context.MapRoute(
"Location_default",
"{culture}/Location/{controller}/{action}/{id}",
new { culture = "en", action = "LocationIndex", id = UrlParameter.Optional },
new { controller = "(LocationIndex)" }
);
时,它会起作用:
Location
但我不希望该网址包含http://localhost/en/LocationIndex/LocationIndex
(区域名称)。我做错了什么?
修改
我要去的网址有点像:
en
此处Home
是当前文化,controller
是Index
名称,action method
是function get_categories() {
$conn = db_connect();
$query = "select * from categories";
$result = $conn->query($query);
if(!$result) {
return false;
}else {
$result = db_result_to_array($result);
return $result;
}
}
名称。
答案 0 :(得分:3)
要使Location
区域成为MVC应用程序的默认路由集,您只需按如下方式定义RouteConfig.cs
:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default_Localized",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { culture = new CultureConstraint(defaultCulture: "en", pattern: "[a-z]{2}") },
namespaces: new string[] { "Locator.Areas.Location.Controllers" }
).DataTokens["area"] = "Location";
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { culture = "en", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Locator.Areas.Location.Controllers" }
).DataTokens["area"] = "Location";
}
}
请注意,这将完全替换应用程序中默认控制器的任何功能,并将所有请求发送到Location
命名空间。
您应该不将任何路由定义放入LocationAreaRegistration.cs
文件中。这将确保它们最后运行,并且不会与您的任何其他区域路线混在一起。
以下是CultureConstraint
的定义。有关如何本地化路线的详细信息,请参阅this answer。
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Routing;
public class CultureConstraint : IRouteConstraint
{
private readonly string defaultCulture;
private readonly string pattern;
public CultureConstraint(string defaultCulture, string pattern)
{
this.defaultCulture = defaultCulture;
this.pattern = pattern;
}
public bool Match(
HttpContextBase httpContext,
Route route,
string parameterName,
RouteValueDictionary values,
RouteDirection routeDirection)
{
if (routeDirection == RouteDirection.UrlGeneration &&
this.defaultCulture.Equals(values[parameterName]))
{
return false;
}
else
{
return Regex.IsMatch((string)values[parameterName], "^" + pattern + "$");
}
}
}