出于某种原因,我的路由忽略了任何访问我的MVC页面的尝试,只是给了我404.我有一个WebForms应用程序设置如下:
虚拟目录:事情
所以我经常这样访问我的网站:
我的ASP.NET WebForms应用程序的原始结构镜像文件系统,所以我有完整的.aspx文件夹,我需要能够像这样使用它们.出于某种原因,当我尝试使用MVC路由访问页面时,例如:
我刚收到404错误.我已经使用了ASP.NET MVC,我知道即使我没有正确设置我的文件夹,我也不会得到404.我会得到无法找到页面的原因并提示文件应该在哪里.以下是我的路由信息.我哪里错了?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute(
"Default",
// Route name
"{controller}/{action}/{id}",
// URL with parameters
new { controller = "Home", action = "Index", id = "" }
// Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
Run Code Online (Sandbox Code Playgroud) 我正在使用一些第三方软件,它们在名称中创建带有连字符的查询字符串参数.我正在研究这个问题,似乎他们的解决方案非常接近我需要的东西,但我对基础MVC的东西太无知了,想弄清楚如何调整它来做我需要的东西.理想情况下,我想简单地用下划线替换连字符,这将是一个很好的解决方案.如果有一个更好的,那么我有兴趣听到它.
我想要处理的URL的一个例子是:
http://localhost/app/Person/List?First-Name=Bob&My-Age=3
Run Code Online (Sandbox Code Playgroud)
使用此控制器:
public ActionResult List(string First_Name, int My_Age)
{
{...}
}
Run Code Online (Sandbox Code Playgroud)
重复一遍,我无法更改生成的查询字符串,所以我需要以某种方式支持它与我的控制器.但是怎么样?
作为参考,下面是自定义RouteHandler,用于处理控制器名称中的下划线和上面引用的SO问题中的操作名称,我们可以修改它以实现我想要的:
public class HyphenatedRouteHandler : MvcRouteHandler
{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString().Replace("-", "_");
requestContext.RouteData.Values["action"] = requestContext.RouteData.Values["action"].ToString().Replace("-", "_");
return base.GetHttpHandler(requestContext);
}
}
Run Code Online (Sandbox Code Playgroud) 我知道如果你认为你在.NET框架中发现了一个错误,那你很可能是错的,但这就是我写这个问题的原因,所以请听我说.
我很确定.NET 3.5和.NET 4.0中的路由在可选参数方面有所不同.特别是如果您的路线中有多个可选参数.我无法在.NET 4.0或MVC 3的任何发行说明中找到这种突破性更改,因此我将其称为错误.
编辑:当您尝试使用代码(如mvc中的url或html帮助程序)构建路径URL时,此错误仅会显示.如果您实际在浏览器中请求网址,在实际的mvc应用程序中,它可以正常工作.因此,如果我下面的测试应用程序是一个真正的mvc应用程序,那么如果您尝试请求'/ root/test1'就不会有问题.
我需要你做的是运行以下测试程序,它应该是相当不言自明的,但基本上它只是设置一个带有一些可选参数的路由.
创建一个新的.NET 4控制台应用程序
将目标框架更改为".NET Framework 4"而不是".NET Framework 4 Client Profile"
添加对以下内容的引用:
System.Web 4.0
System.Web.Routing 4.0
System.Web.Mvc 3.0
将以下代码粘贴到program.cs文件中,覆盖以前的任何内容:
using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
public class Program
{
static void Main()
{
var httpCtx = new HttpContextWrapper(new HttpContext(new HttpRequest(null, "http://localhost/", null), new HttpResponse(new StringWriter())));
var routes = RouteTable.Routes;
routes.MapRoute("Test", "root/{test1}/{test2}/{test3}", new { test2 = UrlParameter.Optional, test3 = UrlParameter.Optional });
var context = new RequestContext(httpCtx , new RouteData()); …Run Code Online (Sandbox Code Playgroud)我已经在StackOverflow上阅读了一些关于此的线程,但无法使其工作.我在Global.asax的RegisterRoutes结尾处有这个.
routes.MapRoute(
"Profile",
"{*url}",
new { controller = "Profile", action = "Index" }
);
Run Code Online (Sandbox Code Playgroud)
基本上我想要实现的是让mydomain.com/Username指向我的成员个人资料页面.我如何设置我的控制器和RegisterRoutes以使其工作?
目前mydomain.com/somethingthatisnotacontrollername出现404错误.
我正在尝试使用以下viewmodel生成Html.ActionLink:
public class SearchModel
{
public string KeyWords {get;set;}
public IList<string> Categories {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
要生成我的链接,我使用以下调用:
@Html.ActionLink("Index", "Search", Model)
Run Code Online (Sandbox Code Playgroud)
其中Model是SearchModel的一个实例
生成的链接是这样的:
http://www.test.com/search/index?keywords=bla&categories=System.Collections.Generic.List
因为它显然只是在每个属性上调用ToString方法.
我想看到的是:
http://www.test.com/search/index?keywords=bla&categories=Cat1&categories=Cat2
有什么方法可以通过使用来实现这一点 Html.ActionLink
我正在构建一个简单的报表Web应用程序,用于使用ASP.NET MVC3 + WebForms呈现报表.报表本身由ReportViewer ASP.NET WebForms控件呈现,但我想使用ASP.NET MVC创建参数条目.
我希望所有请求都遵循'〜/ {controller}/{action}/{parameters}'的默认路由方案,除了请求之外~/Report,应该转到报表呈现WebForm.这样做的正确方法是什么?
扩大一点..
我有两个路由Global.asax.cs- 默认的一个,一个用于WebForms页面.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapPageRoute("report-rendering", "Report", "~/Render.aspx");
}
Run Code Online (Sandbox Code Playgroud)
URL被渲染得很好,但问题在于,当请求进入时,第一个路由也会占用第二个路由的URL,即~/Report?id=7尝试调用该Index方法ReportController(不存在).
如果我更改它以便'报告呈现'路线在'默认'路线之前,如下所示:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("report-rendering", "Report", "~/Render.aspx");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with …Run Code Online (Sandbox Code Playgroud) 我使用路由设置了一个asp.net web api项目(它与Mvc项目完全相同) - 因此我有以下内容
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
所以一切都按照我想要的方式工作....我输入api/Products/15它到达我的产品控制器,传入15作为id.
大.
我还有2个控制器,1个叫做UploadsController,1个叫做DownloadController.这些提供上传和下载(GET/PUT等)
现在我不希望他们被原规则接受(见上文)
但我想要的是使用这两个网址来访问它们
/ api/Transport/Uploads/15/api/Transport/Downloads/15
我已经通过了15作为身份证,可能不会发生在现实生活中...只是它有利于示范:-)
现在运输不存在所以我可以做以下事情
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/Transports/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
我认为有效...
但问题是,如果我这样做
/ api/Uploads/15 - 这也会被我不想要的原始规则所捕获..
我希望通过虚假的"传输"访问上传和DOwnloads控制器,而不是没有传输
有人可以帮忙吗?
提前致谢
asp.net-mvc asp.net-routing asp.net-mvc-routing asp.net-web-api
我必须redirecttoaction在asp.net mvc视图中调用不同的参数,从视图的referrer页面(网格的状态)中提取.
我(在隐藏字段中)查询字符串的内容(有时是空的,有时带有2个参数等等),所以我在创建路由值数组时遇到问题.
是否有一些助手,帮助我将查询字符串转换为路由值数组?就像是:
string querystring ="sortdir=asc&pag=5";
return RedirectToAction( "Index", ConvertToRouteArray(querystring));
Run Code Online (Sandbox Code Playgroud) asp.net-mvc redirecttoaction asp.net-mvc-routing query-string
我在"使用条款"页面的几个翻译的顶部有这个:
<li><a href="@Url.Action("Index", "Terms")">English</a></li>
<li><a href="@Url.Action("Index", "Terms", "de")">Deutsch</a></li>
<li><a href="@Url.Action("Index", "Terms", "fr")">Français</a></li>
<li><a href="@Url.Action("Index", "Terms", "it")">Italiano</a></li>
<li><a href="@Url.Action("Index", "Terms", "nl")">Nederlands</a></li>
<li><a href="@Url.Action("Index", "Terms", "hu")">Maygar</a></li>
<li><a href="@Url.Action("Index", "Terms", "es")">Español</a></li>
<li><a href="@Url.Action("Index", "Terms", "zh")">????</a></li>
<li><a href="@Url.Action("Index", "Terms", "pt-pt")">European Português</a></li>
<li><a href="@Url.Action("Index", "Terms", "pt")">Português</a></li>
Run Code Online (Sandbox Code Playgroud)
这是应该处理点击的操作:
public class TermsController : Controller
{
public ActionResult Index(string id)
{
switch (id)
{
case "de":
return View("de");
case "fr":
return View("fr");
case "it":
return View("it");
case "nl":
return View("nl");
case "hu":
return View("hu");
case "es":
return View("es"); …Run Code Online (Sandbox Code Playgroud) 我想.js使用ASP.NET MVC 5 自动生成一些JavaScript文件,其URL中有适当的扩展名.
这是我的问题;
我在我的网站上使用require.js并且它运行良好.但是,并非所有JavaScript文件都是磁盘上的真实文件.其中一些必须在运行时生成.所以我编写了一个生成动态JavaScript文件的控制器,并在使用默认路由时为它们提供服务;
// ~/Resource/CommonRes -- from ResourceController.CommonRes()
define([], function() {
return {
"menuItemConfiguration":"Configuration",
"menuItemAdmin":"Admin",
"manageAccount":"Manage Account",
"logOff":"Log off"
...
};
});
Run Code Online (Sandbox Code Playgroud)
但是,我需要将路由作为~/Scripts/Resources/CommonRes.js- .js扩展是至关重要的,因为我实际上正在返回一个require.js模块,要像这样调用;
require(['Resources/CommonRes'], function(commonRes) {
// code the uses the resource file
});
Run Code Online (Sandbox Code Playgroud)
在这种情况下,Resources/CommonRes将始终查找名为的模块~/Scripts/Resources/CommonRes.js.因此需要使用该扩展来提供服务.
我似乎无法使路由正确.我尝试过以下但无济于事;
routes.MapRoute(
name: "Resource Scripts",
url: "Scripts/Resource/{action}",
defaults: new { controller = "Resource" },
namespaces: new string[] { "AITrackRecord.Controllers" }
);
Run Code Online (Sandbox Code Playgroud) javascript asp.net-mvc url-routing asp.net-mvc-routing requirejs
asp.net-mvc ×7
asp.net ×2
.net-4.0 ×1
iis ×1
javascript ×1
query-string ×1
requirejs ×1
routing ×1
url-routing ×1