我想使用属性路由。我的代码是:
[Route("api/ws/{parm: myClass}")]
public void Post(myClass parm)
{ ... }
Run Code Online (Sandbox Code Playgroud)
WebApiConfig.Register这导致了如下异常:
“DefaultInlineConstraintResolver”类型的内联约束解析器无法解析以下内联约束:“myClass”。
导致此错误的原因可能是什么?
myClass是一个复杂的对象,即不是一个简单的值类型,而是一个具有多个属性的类。允许这样的参数吗?
我正在寻找能够匹配以下路线的路线定义:
/segment/xxxx/def/segment/.../xxxx/def/segment/that/can/span/xxxx/def并能够xxxx使用参数 `def 运行操作。
但这种路线是不允许的:
[Route("/{*segment}/xxx/{myparam}")]
Run Code Online (Sandbox Code Playgroud)
如何做呢?
asp.net-mvc routes url-routing asp.net-mvc-routing .net-core
假设我有一个UserController动作Details(string id)。
我想创建一个名为的新类,ClientController它继承自UserController.
public class ClientController : UserController
{
}
public class UserController
{
public ActionResult Details(string id)
{
var user = userService.GetById(id);
return View(user);
}
}
Run Code Online (Sandbox Code Playgroud)
当我想向Client/Details/xxxxx发出请求时,问题就出现了。浏览器显示错误
“......用户......详细信息和客户......详细信息之间的操作不明确”
如果我清空客户端类并直接在浏览器上调用 Client/Details/xxxx ,浏览器会显示以下错误:
“未找到视图‘详细信息’或其主视图,或者没有视图引擎支持搜索的位置”
如何让我的 Web 应用程序使用Client/Details/xxxx请求作为User/Details/xxxx而不在 Clients 类中创建代码。
如何映射domain.com/username之类的内容?问题是我认为MVC路由查找控制器以确定它应该如何处理映射请求.
我是ASP.NET MVC的新手.
但是,基于到目前为止的教程,路由机制似乎相当严格.
我的ASP.NET MVC2网站有一些路由问题,并且想知道是否有一种方法可以让运行时到它认为理解的所有路由的简单列表.
像fubu诊断这样的东西会很方便... 替代文字http://guides.fubumvc.com/images/guides1diags.png
如果我的动作有一条路径,/controller/action/{id}我可以id通过这样做获得AuthorizeAttribute httpContext.Request.RequestContext.RouteData.Values["id"].
相反,如果它像/controller/action?id={id}我可以做到的那样httpContext.Request.QueryString["id"].
如果它是来自POST的表单数据,我还需要另一种方式.
有没有办法说"无论路由是如何指定的,都要在名称为'id'的参数中输入内容?"
我正在尝试创建一个路由约束但不确定什么是最好的.这是没有约束的路线:
context.MapRoute(
"Accommodation_accomm_tags",
"accomm/{controller}/{action}/{tag}",
new { action = "Tags", controller = "AccommProperty" },
new { tag = @"" } //Here I would like to put a RegEx for not null match
);
Run Code Online (Sandbox Code Playgroud)
什么是最好的解决方案?
什么是避免在JavaScript中硬编码URL的最佳方法(主要用于进行AJAX调用时)?
在过去:
@Url.Action或的结果呈现JavaScript变量@Url.RouteUrl@Url.Action或@Url.RouteUrl成JavaScript在初始化/构造函数.有没有更好的办法?
做这样的事情会很好:
var url = $.routes("actionName", "controllerName") // or "routeName" for named routes
$.post(url, { id = 1 }, function() { //.. });
Run Code Online (Sandbox Code Playgroud)
这当然不可能实现(JavaScript没有直接访问ViewContext,因此无法访问路由表).
但我想知道是否有一种方法可以为JavaScript设置我自己的"路由表",只有我知道它需要的那些?(例如我在视图中设置)
人们如何处理这个问题?
javascript asp.net-mvc asp.net-mvc-routing razor asp.net-mvc-3
我不知道为什么在调试项目后出现此错误,即使代码是默认的.
调节器
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
视图
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<div>
Hi
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
不知何故,在调试之后,请求的URL始终是/Views/Home/Index.cshtml,但通过浏览器访问Home是可以的.(HTTP://本地主机:58323 /家)
我用谷歌搜索,解决方案暗示问题在于全球.但那很奇怪,我不记得对它进行任何改动.
全球
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
);
}
Run Code Online (Sandbox Code Playgroud)
感谢任何帮助.谢谢
我有一个失控的请求控制器,我想在几个控制器上划分操作,同时保持一个干净的URL.我正在尝试路由,但没有成功.我已经阅读了一些关于路由的示例和教程,但是,虽然我理解这些示例,但似乎没有什么适用于我的情况,我觉得不是更明智.我想要的是Requests/Approval在我的ApprovalController而不是我的RequestController上处理URL ,所以我写了以下内容.
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.MapRoute(
"Approval",
"Request/{controller}/{action}",
new { controller = "Approval", action="Index", id = "" }
);
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.为什么?我的视图中有一个名为Approval的文件夹,在那里我有一个名为的文件Index.cshtml.我该如何编写MapRoute代码?
编辑
我添加了我所有的路线
asp.net-mvc ×9
asp.net ×3
c# ×2
.net-core ×1
inheritance ×1
javascript ×1
razor ×1
regex ×1
routes ×1
url-routing ×1