ASP.NET MVC 3 路由

发布于 2024-11-02 21:24:52 字数 780 浏览 4 评论 0原文

我正在尝试创建路线。

例如

/emlak/TITLE/number.aspx

Global.asax

/emlak/Here_is_your_best_property/123456.aspx

routes.MapRoute(
    "Product",
    "{controller}/{deli}/{productId}",
    new { controller = "emlak", action = "Index" },
    new { productId = UrlParameter.Optional , deli = UrlParameter.Optional  }
);

我的控制器

namespace emrex.Controllers
{
    public class EmlakController : Controller
    {
        //
        // GET: /Emlak/

        public ActionResult Index(String productId, String deli)
        {
            return View();
        }

    }
}

和我收到下一个错误:

“/”应用程序中的服务器错误。

找不到资源。

感谢您的帮助。

i am trying to create route.

Which is

/emlak/TITLE/number.aspx

such as

/emlak/Here_is_your_best_property/123456.aspx

Global.asax:

routes.MapRoute(
    "Product",
    "{controller}/{deli}/{productId}",
    new { controller = "emlak", action = "Index" },
    new { productId = UrlParameter.Optional , deli = UrlParameter.Optional  }
);

My controller

namespace emrex.Controllers
{
    public class EmlakController : Controller
    {
        //
        // GET: /Emlak/

        public ActionResult Index(String productId, String deli)
        {
            return View();
        }

    }
}

and i am getting next error:

Server Error in '/' Application.

The resource cannot be found.

Thanks for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

べ映画 2024-11-09 21:24:52

不要提供 URL 参数默认值作为约束(就像您所做的那样)

当您将路线定义为(我添加了附加注释,以便我们知道每个部分是什么)时

routes.MapRoute(
    // route name
    "Product",

    // Route URL definition
    "{controller}/{deli}/{productId}",

    // route values defaults
    new { controller = "emlak", action = "Index" },

    // route values constraints
    new { productId = UrlParameter.Optional , deli = UrlParameter.Optional  }
);

,所以基本上您不应该在您的情况下提供约束,这使得它毫无意义。将最后两个放在路由默认值中,并保留此路由定义之外的约束:

routes.MapRoute(
    "Product",
    "{controller}/{deli}/{productId}",
    new {
        controller = "Emlak",
        action = "Index",
        productId = UrlParameter.Optional,
        deli = UrlParameter.Optional
    }
);

这绝对应该有效,除非您有其他路由定义或不使用您提供的代码。

Don't provide URL parameter defaults as constraints (as you did)

When you define your route as (I added additional comments so we know what each part is)

routes.MapRoute(
    // route name
    "Product",

    // Route URL definition
    "{controller}/{deli}/{productId}",

    // route values defaults
    new { controller = "emlak", action = "Index" },

    // route values constraints
    new { productId = UrlParameter.Optional , deli = UrlParameter.Optional  }
);

So basically you shouldn't provide constraints in your case which makes it meaningless. Put the last two in route defaults and keep constraints out of this route defintion as:

routes.MapRoute(
    "Product",
    "{controller}/{deli}/{productId}",
    new {
        controller = "Emlak",
        action = "Index",
        productId = UrlParameter.Optional,
        deli = UrlParameter.Optional
    }
);

This should definitely work unless you have some other route definitions or don't use code that you provided.

一笔一画续写前缘 2024-11-09 21:24:52

你的问题是(至少当我尝试你的代码时)你在不应该的地方指定了路由约束。我可以通过这样做来让它正常工作:

     routes.MapRoute(
    "Product",
    "{controller}/{deli}/{productId}",
    new { controller = "emlak", action = "Index", productId = UrlParameter.Optional, deli = UrlParameter.Optional }
);

尝试一下 - 有什么区别吗?

Your problem is (at least when I tried your code) you have route constraints specified where they really shouldn't be. I was able to get this to work just fine by doing:

     routes.MapRoute(
    "Product",
    "{controller}/{deli}/{productId}",
    new { controller = "emlak", action = "Index", productId = UrlParameter.Optional, deli = UrlParameter.Optional }
);

Try that - any difference?

流心雨 2024-11-09 21:24:52

可能有帮助,因为我还没有从 MVC 1.0 升级...

我认为您不需要 URL 的 .aspx 部分,因为 MVC 以不同的方式处理应用程序实例化。如果使用 IIS 6,您还需要 .mvc 扩展名(例如“emlak.mvc/TITLE/number”); IIS 7 应使用“emlak/TITLE/number”正确实例化。

This may help, as I haven't upgraded from MVC 1.0 yet...

I don't think you need the .aspx portion of the URL because MVC handles application instantiation differently. Also you need a .mvc extension if using IIS 6 (e.g. "emlak.mvc/TITLE/number"); IIS 7 should instantiate correctly with "emlak/TITLE/number".

浅黛梨妆こ 2024-11-09 21:24:52

您应该删除约束并提供“productId”和“deli”的默认值。

routes.MapRoute(
    "Product",
    "{controller}/{deli}/{productId}",
    new { controller = "emlak", action = "Index", productId = 123 , deli = "xyz"  }
);

或者

在控制器的操作中使参数可选

public ActionResult Index(String productId = 0, String deli = "")
{
    return View();
}

You should remove the constraints and provide the deafults for "productId" and "deli".

routes.MapRoute(
    "Product",
    "{controller}/{deli}/{productId}",
    new { controller = "emlak", action = "Index", productId = 123 , deli = "xyz"  }
);

OR

make your parameters optional at action in your controller

public ActionResult Index(String productId = 0, String deli = "")
{
    return View();
}
轮廓§ 2024-11-09 21:24:52

您的操作要求同时提供 deli 和 ProductId,并且您的路线不提供两者的默认值。添加不需要提供任何值的索引操作,或者为变量添加默认值。

辅导员本

Your action requires that deli and productId both be supplied, and your route does not supply default values for either. Either add an Index action which does not require any values be supplied, or add default values for your variables.

counsellorben

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文