MVC Razor URlHelper.Action 路径问题中的自定义路由
我的应用程序有自定义路由,如下所示
应用程序名称是 ValidationTest 并且我确实将默认绑定更改为以下 . 这工作正常,但我确实在控制器类中有一些操作,并且我确实使用 urlHelper 来识别操作路径和控制器路径您可以在底部代码中看到这一点,但是在我更改默认路由后,它会抛出异常从 url helper 读取操作,我是 mvc 新手,所以请建议我如何给出控制器和操作的路径
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"MyRout",// Route name
"RubinsApp/{CRM}/{id}",// URL with parameters
new {
controller = "Render",
action = "RenderApp",
id = UrlParameter.Optional
} // Parameter defaults
);
}
,这样我的 url 就是
http://localhost/ValidationTest/RubinsApp/crm/test
这样
ValidationTest=Application Name
RubinsApp/CRM =Routing Para
test=id
,这工作正常 我的问题是,我有一个 Ajax 操作调用,其中操作 URl 的定义如下所示 在cshtml
UrlHelper urm = new UrlHelper(Request.RequestContext);
var urlsMenu = urm.Action("BuildNavigationMenu", "Render").ToString(); //This Thorws Object null Error
这里
BuildNavigationMenu=Action Name
Render= Controller
I have Custom routing for my application as shown bellow
Application Name is ValidationTest and i do changed my default binding to following .
That works fine , But i do have some Actions in Controller class , and i do use urlHelper to Identify the Action Path and Controller path You can see that in the Bottom code , But after i changed the default routing it throws me exception While trying to read action from url helper , and i am new to mvc so please suggest me how to give the path to Controller and Action
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"MyRout",// Route name
"RubinsApp/{CRM}/{id}",// URL with parameters
new {
controller = "Render",
action = "RenderApp",
id = UrlParameter.Optional
} // Parameter defaults
);
}
so my url is
http://localhost/ValidationTest/RubinsApp/crm/test
so
ValidationTest=Application Name
RubinsApp/CRM =Routing Para
test=id
and this works Fine
My Problem is, I have an Ajax Action Call Where The Action URl was Defined Like Bellow
in cshtml
UrlHelper urm = new UrlHelper(Request.RequestContext);
var urlsMenu = urm.Action("BuildNavigationMenu", "Render").ToString(); //This Thorws Object null Error
Here
BuildNavigationMenu=Action Name
Render= Controller
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的自定义路由中,您已删除了
{action}
标记,这意味着除了默认定义的RenderApp
之外,您无法再指定任何其他操作。这就是urlHelper.Action
方法返回 null 的原因。根据您定义路由的方式,在此应用程序中您只能在单个控制器 (
Render
) 上执行单个操作 (RenderApp
)。在 urlHelper.Action 调用中,您尝试调用BuildNavigationMenu
操作,但显然这是不可能的,因为永远无法到达此操作。因此,您必须修改
"RubinsApp/{CRM}/{id}"
路由,以在某处至少包含{action}
令牌。或者在其后添加另一个路由定义。只是关于您的代码的旁注。如果您在控制器操作中编写此内容,则无需实例化新的
UrlHelper
。 Controller 类已经有一个Url
属性:In your custom route you have removed the
{action}
token meaning that you can no longer specify any other action than the one defined by default which isRenderApp
. That's why theurlHelper.Action
method returns null.The way you have defined your routes, in this application you could only ever execute a single action (
RenderApp
) on a single controller (Render
). In your urlHelper.Action call you are attempting to invoke theBuildNavigationMenu
action but obviously that's impossible as this action can never be reached.So you will have to modify your
"RubinsApp/{CRM}/{id}"
route to include at least the{action}
token somewhere. Or add another route definition after it.And just a side-note about your code. If you are writing this inside a controller action you don't need to instantiate a new
UrlHelper
. The Controller class already has aUrl
property: