标签: asp.net-mvc-routing

html.routelink 不去指定路由

我有一堆路由定义为:

routes.MapRoute(
                name: "ID",
                url: "{category}/{subcategory}/{lowercategory}/{lowercategory1}/{id}/{ignore}",
                defaults: new { controller = "Home", action = "Index", ignore = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "LowerCategory1",
                url: "{category}/{subcategory}/{lowercategory}/{lowercategory1}",
                defaults: new { controller = "Home", action = "Index" }
            );

            routes.MapRoute(
                name: "LowerCategory",
                url: "{category}/{subcategory}/{lowercategory}",
                defaults: new { controller = "Home", action = "Index" }
            );

            routes.MapRoute(
                name: "Subcategory",
                url: "{category}/{subcategory}",
                defaults: new { controller = "Home", action = "Index" }
            );

            routes.MapRoute(
                name: "Category",
                url: "{category}",
                defaults: new { controller = "Home", action …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc routing asp.net-mvc-routing

5
推荐指数
1
解决办法
725
查看次数

在 ASP.NET MVC 中创建两条不会产生 404 错误的路由的问题

我正在尝试使用路由构建我的教程项目。我的主要目标是构建两条在任何情况下都不会产生 404 错误的路由。我的意思是,如果路径错误,我希望路由使用 /Home/Index 路径。我有以下两条路线 -

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

    routes.MapRoute("Second", "{*catchall}",
                        new {controller = "Home", action = "Index", id = UrlParameter.Optional}
                        );
Run Code Online (Sandbox Code Playgroud)

当我使用与第一条路线不匹配的不存在的路径时,它工作正常,就像这样 -

不存在的路径 1

但如果是这样,那么我有以下几点——

在此处输入图片说明

或者

在此处输入图片说明

我明白它发生的原因。但是目前,我只能设法找到“某种”解决方案。将以下代码添加到 web.config 文件 -

<customErrors mode="On">
      <error statusCode="404" redirect="~/Home/Index"/>
</customErrors>
Run Code Online (Sandbox Code Playgroud)

但是,我认为这不是解决此问题的最佳方法。因为,据我所知,它只是捕获所有错误并将其重定向到正确的路径,而不实际使用路由。所以我认为我不需要这种全局处理。

所以有人可以给我一个提示或为我提供一个很好的解决我的问题的方法。谢谢你。

c# asp.net-mvc routes web-config asp.net-mvc-routing

5
推荐指数
1
解决办法
1176
查看次数

Subdomain on localhost gives Bad Request - Invalid Hostname error

I am using MVC. I want to test subdomains on localhost with IIS. What I have done to create a subdomain is:

  1. I added a line to windows host file

    127.0.0.1       localhost
    127.0.0.1       abc.localhost
    ::1             localhost
    
    Run Code Online (Sandbox Code Playgroud)
  2. I edited applicationhost.config as:

     <bindings>
           <binding protocol="http" bindingInformation="*:59322:localhost" />
           <binding protocol="http" bindingInformation="*:59322:abc.localhost" />
     </bindings>
Run Code Online (Sandbox Code Playgroud)
  1. I added following class to RouteConfig.cs :

    public class SubdomainRoute : RouteBase { public override RouteData GetRouteData(HttpContextBase httpContext) { var host = httpContext.Request.Url.Host; var index = host.IndexOf("."); string[] segments = …

c# asp.net subdomain asp.net-mvc asp.net-mvc-routing

5
推荐指数
1
解决办法
1363
查看次数

应用程序根的默认路由

我怎样才能告诉我的mvc-application 路由到特定的Controller以及Action何时未指定?

调试http://localhost:54500/时应路由到http://localhost:54500/Home/Index.

目前我有:

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

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional }
    );
Run Code Online (Sandbox Code Playgroud)

但这总是抛出

未找到视图“索引”或其主视图或没有视图引擎支持搜索的位置

编辑 #1

它应该重定向/路由到驻留在Area被调用的视图中Home。只是想澄清一下,有 aController和 anArea两者都被命名为Home

该区域的配置是:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Home_default",
        "Home/{controller}/{action}/{id}",
        new {action = "Index", id = UrlParameter.Optional}
        );
}
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-mvc routes url-routing asp.net-mvc-routing

5
推荐指数
1
解决办法
8046
查看次数

向 HttpDelete 操作发送 DELETE 请求时路由不起作用

我有一个只有一个视图的 ProductsController - Index.cshtml。

以下 3 个操作方法位于此控制器内部:

//http://localhost:55555/products
[HttpGet]
public IActionResult Index()
{
}

//http://localhost:55555/products
[HttpPost]
public IActionResult Index(ProductViewModel product)
{
}

//http://localhost:55555/products/1
[HttpDelete("{id}")]
public IActionResult Index([FromRoute] int id)
{
}
Run Code Online (Sandbox Code Playgroud)

当我转到 /products 时,Get 工作得很好。当我使用 /products 创建新产品时,Post 工作得非常好。删除根本不起作用,我在 /products/9 中找不到 404。我正在使用标准的 AJAX 请求,类型为:DELETE。

我正在使用默认的 MVC 常规路由设置:

        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
Run Code Online (Sandbox Code Playgroud)

这是我的 AJAX 请求的样子:

$(".js-delete-product").click(function () {
    $.ajax({
        type: "DELETE",
        url: "products/" + $(this).data("id"),
        success: onDeleteSuccess
    });
});
Run Code Online (Sandbox Code Playgroud)

我还尝试在邮递员中向http://localhost:55555/products/1发送 DELETE 请求,以确保它不是我的 jquery ajax 并且仍然找不到 404。

更新: 如果我仅向http://localhost:55555/products …

c# asp.net-mvc asp.net-mvc-routing asp.net-mvc-controller asp.net-core-mvc

5
推荐指数
1
解决办法
1万
查看次数

如何摆脱由 MVCAttribute 路由引起的这个错误?

我在约定路由旁边启用了 MVC 属性路由。每次运行应用程序时都会出现此错误。

“DefaultInlineConstraintResolver”类型的内联约束解析器无法解析以下内联约束:“字符串”。说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.InvalidOperationException:“DefaultInlineConstraintResolver”类型的内联约束解析器无法解析以下内联约束:“string”。

这是堆栈跟踪:

[InvalidOperationException异常:类型DefaultInlineConstraintResolver'的内联约束解算器无法解决以下内嵌约束:“字符串”]
System.Web.Mvc.Routing.InlineRouteTemplateParser.GetInlineConstraint(集团constraintGroup,布尔isOptional,IInlineConstraintResolver constraintResolver)389
系统.Web.Mvc.Routing.InlineRouteTemplateParser.ParseRouteTemplate(字符串routeTemplate,IDictionary的2 defaults, IDictionary2个约束,IInlineConstraintResolver constraintResolver)488
System.Web.Mvc.Routing.DirectRouteFactoryContext.CreateBuilder(字符串模板,IInlineConstraintResolver constraintResolver)308
System.Web.Mvc.Routing .DirectRouteFactoryContext.CreateBuilderInternal(字符串模板)+48
System.Web.Mvc.Routing.DirectRouteFactoryContext.CreateBuilder(String template) +44
System.Web.Mvc.RouteAttribute.System.Web.Mvc.Routing.IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context) +80
System.Web.Mvc.Routing。 DefaultDirectRouteProvider.CreateRouteEntry(String areaPrefix, String controllerPrefix, IDirectRouteFactory factory, IReadOnlyCollection 1 factory, IReadOnlyCollection 1 factory, IInlineConstraintResolverconstraintResolver) +188 System.Web.Mvc.Routing.DefaultDirectRouteProvider.GetDirectRoutes(ControllerDescriptor controllerOnlyConstraintProvider, IsolvRoute controllerOntrainResolver, Isolver controllerOnlyResolver, IReadOnlyCollection 1 ) +2341 actions, IInlineConstraintResolver constraintResolver, Boolean targetIsAction) +115
System.Web.Mvc.Routing.DefaultDirectRouteProvider.CreateRouteEntries(String areaPrefix, String controllerPrefix, IReadOnlyCollection
1 actions, IInlineConstraintResolver constraintResolver, Boolean targetIsAction) +155
System.Web.Mvc.Routing.DefaultDirectRouteProvider.GetActionDirectRoutes(ActionDescriptor actionDescriptor, IReadOnlyList

1 actionDescriptors, IInlineConstraintResolver constraintResolver) +245
System.Web.Mvc.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection …

c# asp.net-mvc asp.net-mvc-routing attributerouting

5
推荐指数
1
解决办法
1263
查看次数

ASP.NET MVC 5 Web 应用程序性能下降

我正在开发一个新的 ASP.NET MVC Web 应用程序,我看到所有页面都需要大约 12-15 秒才能加载,而不管控制器或视图中有什么。我有大约需要 8 秒的页面,除了 return View() 和在 html 中仅使用页面名称查看页面之外,控制器中没有代码。

我运行了 SQL Profiler 并且响应非常快,所以看起来 MVC 路由或其他东西一直在占用。到目前为止,我尝试了以下方法:

  1. 从 global.asax 中删除所有视图引擎并仅使用 Razor 视图引擎
  2. 为 css 和 js 启用捆绑和缩小
  3. 在 IIS 7 下为静态和动态内容启用压缩
  4. 我在我们的开发服务器上部署了 Release Build,但性能仍然相同

请让我知道 MVC 应用程序性能缓慢的原因可能是什么。

另一方面,对于大约有 150-250 个用户同时浏览的企业 Web 应用程序,我应该拥有的最佳服务器配置是什么,例如 CPU、处理器、RAM 等。

asp.net-mvc performance iis-7 asp.net-mvc-routing windows-server-2012

5
推荐指数
1
解决办法
2805
查看次数

将 List&lt;int&gt; 从 actionlink 传递到控制器方法

在我的控制器中,我有这个:

ViewBag.lstIWantToSend= lstApps.Select(x => x.ID).ToList();  // creates a List<int> and is being populated correctly
Run Code Online (Sandbox Code Playgroud)

我想将该列表传递给另一个控制器..所以在我看来我有:

@Html.ActionLink(count, "ActionName", new { lstApps = ViewBag.lstIWantToSend }, null)
Run Code Online (Sandbox Code Playgroud)

控制器中的方法:

public ActionResult ActionName(List<int> lstApps)   // lstApps is always null
Run Code Online (Sandbox Code Playgroud)

有没有办法将整数列表作为路由值发送到控制器方法?

asp.net-mvc asp.net-mvc-routing html.actionlink

5
推荐指数
1
解决办法
4443
查看次数

ASPCore 中间件中的当前 URL?

有没有办法可以在 ASPCore 2.0 中间件中访问当前请求的 URL?

有什么我可以注射的吗?

url-routing asp.net-mvc-routing asp.net-core-mvc asp.net-core

5
推荐指数
1
解决办法
4225
查看次数

如何在每个操作的基础上覆盖 RouteOptions.LowercaseUrls?

在我的 ASP.NET Core 2.1 Web 应用程序中,我的 Startup 方法中有这个:

services.Configure<RouteOptions>( o =>
{
    o.LowercaseUrls = true;
} );
Run Code Online (Sandbox Code Playgroud)

对于初学者,此设置会导致UrlHelper以小写形式呈现所有路径组件(但不是查询字符串项)。

例如,给定这个控制器和动作:

public class FooController : Controller
{
    [Route("/FOO/bAR/{baz}")]
    public IActionResult Bar( [FromRoute] String baz, [FromQuery] String qux )
    {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

并给出了这个 Razor 表达式:

@Url.Action( action: nameof(FooController.Bar), new { baz = "Ab", qux = "Ab" } )
Run Code Online (Sandbox Code Playgroud)

有了o.LowercaseUrls = false那么这将呈现:

/FOO/bAR/Ab?qux=Ab
Run Code Online (Sandbox Code Playgroud)

有了o.LowercaseUrls = true那么这将呈现:

/foo/bar/ab?qux=Ab
Run Code Online (Sandbox Code Playgroud)

LowercaseUrls = true对于 90% 的操作,使用是很好的(甚至是首选!),但对于少数 URL,重要的是保留路由参数的大小写(特别是当路由参数是 Base64 编码或 …

routing asp.net-mvc-routing asp.net-core

5
推荐指数
0
解决办法
362
查看次数