MVC Razor URlHelper.Action 路径问题中的自定义路由

发布于 2025-01-04 23:05:56 字数 1181 浏览 1 评论 0原文

我的应用程序有自定义路由,如下所示

应用程序名称是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左秋 2025-01-11 23:05:57

在您的自定义路由中,您已删除了 {action} 标记,这意味着除了默认定义的 RenderApp 之外,您无法再指定任何其他操作。这就是 urlHelper.Action 方法返回 null 的原因。

根据您定义路由的方式,在此应用程序中您只能在单个控制器 (Render) 上执行单个操作 (RenderApp)。在 urlHelper.Action 调用中,您尝试调用 BuildNavigationMenu 操作,但显然这是不可能的,因为永远无法到达此操作。

因此,您必须修改 "RubinsApp/{CRM}/{id}" 路由,以在某处至少包含 {action} 令牌。或者在其后添加另一个路由定义。

只是关于您的代码的旁注。如果您在控制器操作中编写此内容,则无需实例化新的 UrlHelper。 Controller 类已经有一个 Url 属性:

public ActionResult Foo()
{
    var urlsMenu = Url.Action("BuildNavigationMenu", "Render");
    ...
}

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 is RenderApp. That's why the urlHelper.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 the BuildNavigationMenu 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 a Url property:

public ActionResult Foo()
{
    var urlsMenu = Url.Action("BuildNavigationMenu", "Render");
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文