无法在ASP.NET MVC中指定路由。

huangapple go评论84阅读模式
英文:

Unable to specify route in ASP.NET MVC

问题

I am new to ASP.NET MVC. I am using visual studio to setup a small project. But there is no app_start folder so I can make changes to routeconfig file. There is a program.cs file where it contained the routecontroller, but there is no effect on it. The default method still remains Index.

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
    name: "Read",
    pattern: "{controller=Read}/{action=Welcome}/{id?}");

So I created a folder with routeconfig file, but it doesn't recognize the ignore route method and unable to run it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace SecondProject
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

Kindly help.

英文:

I am new to ASP.NET MVC. I am using visual studio to setup a small project. But there is no app_start folder so I can make changes to routeconfig file. There is a program.cs file where it contained the routecontroller, but there is no effect on it. The default method still remains Index.

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
    name: "Read",
    pattern: "{controller=Read}/{action=Welcome}/{id?}");

So I created a folder with routeconfig file, but it doesn't recognizes the ignore route method and unable to run it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace SecondProject
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

Kindly help.

答案1

得分: 1

这不适用于评论。因此,我将其作为答案发布。

您的问题包含与不同版本的ASP.NET相关的代码部分。第一个与ASP.NET Core MVC相关,第二个与ASP.NET Framework MVC相关。这些是具有不同路由实现的不同版本。
在构建项目时,您应该使用正确的模板 - 这取决于您的要求。

问题是:您正在使用哪个版本?

如果您的项目不包含app_start文件夹,但包含program.cs文件,这意味着您正在使用ASP.NET Core MVC。然后,请查看文档,了解如何在您的应用程序中实现路由:在ASP.NET Core中路由到控制器操作

要将Welcome操作方法设置为默认操作,请删除第一个路由模板声明,仅保留"Read"模式:

app.MapControllerRoute(
    name: "Read",
    pattern: "{controller=Read}/{action=Welcome}/{id?}");
英文:

This doesn't fit to a comment. Therefore, I put it as an answer.

Your question contains parts of code related to different versions of the ASP.NET. The first related to the ASP.NET Core MVC and the second for ASP.NET Framework MVC. These are different version with different routing implementation.
You should use the correct template when you build your project - this depend on your requirements.

The question is: Which version you are using?

If your project doesn't contain app_start folder, but contains program.cs file this means that you are using ASP.NET Core MVC. Then look at the documentation hot to implement routing in your application: Routing to controller actions in ASP.NET Core

<hr>

To make the Welcome action method as the default remove the first route template declaration and leave only the &quot;Read&quot; pattern:

app.MapControllerRoute(
    name: &quot;Read&quot;,
    pattern: &quot;{controller=Read}/{action=Welcome}/{id?}&quot;);

huangapple
  • 本文由 发表于 2023年6月29日 14:55:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定