简体   繁体   English

ASP.Net MVC 路由问题 url 不工作

[英]ASP.Net MVC routing issue url not working

I created two action methods with a different name in the home controller我在家庭控制器中创建了两个具有不同名称的操作方法

public ActionResult Default()
{
    ViewData["Message"] = "Welcome to ASP.NET MVC!";
    return View("index");
}

public ActionResult Index(int a)
{
    ViewData["Message"] = "Welcome to ASP.NET MVC! and Your Age is " + a;
    return View();
}

And my routing code look like:我的路由代码如下所示:

routes.MapRoute(
  "Default1", // Route name
  "{Home}/{ID}", // URL with parameters
  new { controller = "Home", action = "Index", id =UrlParameter.Optional});

routes.MapRoute(
    "Default2", // Route name
    "{Home}", // URL with parameters
    new { controller = "Home", action = "Default" }
);

routes.MapRoute(
    "Default", // Route name
    "{controller}", // URL with parameters
    new { controller = "Home", action = "Default" }
);

But still getting problem但仍然遇到问题

when I type URL like http://localhost:7221 then home is coming and Default() method invoking but if I type URL like当我输入像http://localhost:7221这样的 URL 然后 home 来了并且调用 Default() 方法但是如果我输入像这样的 URL

http://localhost:7221/Home then getting error. http://localhost:7221/Home然后出现错误。 to handle this situation i define route like为了处理这种情况,我定义了这样的路线

routes.MapRoute(
    "Default2", // Route name
    "{Home}", // URL with parameters
    new { controller = "Home", action = "Default" }
);

But it is not working.......can u tell me why.但它不起作用.......你能告诉我为什么吗?

If I type URL like http://localhost:7221/Home/88 then Index(int a) method should be called but getting error.如果我键入http://localhost:7221/Home/88 之类的 URL,则应该调用 Index(int a) 方法但出现错误。 why为什么

I want that when I type URL http://localhost:7221 or http://localhost:7221/Home then Default() should be called and when I will type我希望当我键入 URL http://localhost:7221http://localhost:7221/Home时应该调用 Default() 以及我何时键入

http://localhost:7221/Home/88 then Index(int a) should be invoked. http://localhost:7221/Home/88然后应该调用 Index(int a) 。 What is wrong in my route?我的路线有什么问题? How can I rectify it?我该如何纠正它? if possible rectify my route code.如果可能的话,纠正我的路线代码。 thanks谢谢

This rule should actually cover everything you need:这条规则实际上应该涵盖你需要的一切:

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{a}",
            new {
                controller = "Home",
                action = "Index",
                a = UrlParameter.Optional 
            }
        );

Note the following:请注意以下事项:

  1. The parameter name needs to match the parameter on the method, you had id and a - so the framework can't match them.参数名称需要与方法上的参数匹配,您a id和 - 所以框架无法匹配它们。 In the rule above, I have put a in the mapping rule to match your method, but you should give it a better name in both places.在上面的规则中,我在映射规则中放a来匹配你的方法,但是你应该在两个地方都给它起一个更好的名字。

  2. This rule defaults to /Home/Index if no controller or action are supplied.如果没有提供控制器或操作,则此规则默认为 /Home/Index。 If you want to hit the action you have named Default you would go to /Home/Default如果你想点击你命名为Default的动作,你会去 /Home/Default

  3. If you supply /Home/Index/21 it will call the Index method - but if you don't supply an age it will have a problem as you have no method to match the rule.如果您提供 /Home/Index/21 ,它将调用 Index 方法 - 但如果您不提供年龄,它将出现问题,因为您没有方法来匹配规则。 You need to add a method public ActionResult Index() or use a default value public ActionResult Index(int a = 0)您需要添加方法public ActionResult Index()或使用默认值public ActionResult Index(int a = 0)

Here are some examples of URLs that should work against this rule.以下是一些不符合此规则的 URL 示例。

  • http://yourapp/ - will go to /Home/Index http://yourapp/ - 将转到 /Home/Index
  • http://yourapp/Home - will go to /Home/Index http://yourapp/Home - 将转到 /Home/Index
  • http://yourapp/Home/Default - will go to /Home/Default http://yourapp/Home/Default - 将转到 /Home/Default
  • http://yourapp/Home/Index - see my notes above - you need a method to support this http://yourapp/Home/Index - 看我上面的笔记 - 你需要一个方法来支持这个
  • http://yourapp/Home/Index/21 - will go to /Home/Index and pass a as 21 http://yourapp/Home/Index/21 - 将转到 /Home/Index 并将a作为 21 传递

The reason why you are having problems is that your routeparameters don't match with the action method.您遇到问题的原因是您的路由参数与操作方法不匹配。 Your action method requires int a, yet you are not providing it in any case, you are giving integer called id.您的操作方法需要 int a,但无论如何您都没有提供它,而是给出了一个名为 id 的整数。

What you should have in routevalues is:你应该在 routevalues 中拥有的是:

        routes.MapRoute(
            name: "Home",
            url: "{controller}/{age}/{action}",
            defaults: new { controller = "Home", action = "Index", age = UrlParameter.Optional }
        );

Note how age parameter is optional.请注意年龄参数是可选的。 Our controller should then look like this:我们的控制器应该是这样的:

public class HomeController : Controller
{

    public ActionResult Index(int? age)
    {
        if ( age != null )
            ViewData["Message"] = "Welcome to ASP.NET MVC! and Your Age is " + age;
        else
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

        return View();
    }
}

Ofcourse we could use parameter called id but I called it age for demonstration purposes.当然我们可以使用名为 id 的参数,但出于演示目的我将其称为 age。

Update:更新:

Here's something closer to your original request:这是更接近您的原始请求的内容:

        routes.MapRoute(
            name: "Index_age",
            url: "{controller}/{age}/{action}/{id}",
            defaults: new { controller = "Home", action = "IndexA", id = UrlParameter.Optional }
        );
        routes.MapRoute(
            name: "Index",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

Controller:控制器:

    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
        return View();
    }
    public ActionResult IndexA(int age)
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC! and Your Age is " + age;
        return View("Index");
    }

Remove the braces arround Home, change ID to Age and remove the optional behavior of Age.移除 Home 周围的大括号,将 ID 更改为 Age 并移除 Age 的可选行为。

routes.MapRoute(
    "Default1", // Route name
     "Home/{Age}", // URL with parameters
     new { controller = "Home", action = "Index" }
);

I hope I uderstood what you are trying to acheive properly.我希望我能理解您要正确实现的目标。 If not, I suggest using a Route debugger to understand further.如果没有,我建议使用路由调试器来进一步了解。

Try https://github.com/Haacked/RouteMagic (Available on NuGet as RouteMagic.Mvc)试试https://github.com/Haacked/RouteMagic (在 NuGet 上作为 RouteMagic.Mvc 可用)

I haven't tried it myself but I do use one component of it.我自己没有尝试过,但我确实使用了其中的一个组件。

Good luck!祝你好运!

What are the errors that you are getting?你得到的错误是什么? I just don't want to assume anything here.我只是不想在这里假设任何事情。 I think the //localhost:7221/Home call is failing because it is being matched against the first route (since the id param is optional) and not the second one as you are expecting it to.我认为 //localhost:7221/Home 呼叫失败是因为它与第一条路线匹配(因为 id 参数是可选的)而不是您期望的第二条路线。 Also the name of the parameter in the route definition should match the name of the parameter in the action method declaration.此外,路由定义中的参数名称应与操作方法声明中的参数名称相匹配。

if i type url like //localhost:7221/Home/88 then Index(int a) method should be called but getting error.如果我输入类似 //localhost:7221/Home/88 的 url,那么应该调用 Index(int a) 方法但出现错误。 why为什么

rename the parameter to id or the route optional parameter to a.将参数重命名为 id 或将路由可选参数重命名为 a。 The routing engine can not find anything to deduce the value of "a" from so it tries to pass a null value in place of a none nullable int parameter hence the error.路由引擎找不到任何可以从中推断出“a”值的东西,因此它尝试传递空值来代替不可为空的 int 参数,因此出现错误。

i want that when i type url //localhost:7221 or //localhost:7221/Home then Default() should be called我希望当我输入 url //localhost:7221 或 //localhost:7221/Home 时应该调用 Default()

Then move the mapping of the last route to the top of the code block like that it will be added first to the routes collection and localhost:7221/Home will be matched against it first.然后将最后一条路由的映射移动到代码块的顶部,这样它将首先添加到路由集合中,并且 localhost:7221/Home 将首先与它匹配。

You should also look into this tool by Phil Haack:您还应该看看 Phil Haack 的这个工具:

http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM