是否可以路由分层路径以映射数据库中的关系,如下所示:
假设我有一个元组/实体"页面"与"页面"(本身)的mtm关系,我希望能够将每个页面的slug值组合起来找到一个合适的页面,如下所示:
mydomain.com/firstpage/secondpage/thirdpage
where firstpage,secondpageand thirdpage是"page"类型,第三页引用第二页等.
您将如何使用ASP.NET MVC路由实现此功能?
我正在尝试创建一个全能路由来跟踪联盟字符串何时在URL中.联盟代码由a x后跟a 标记int,并且仅出现在URL的末尾(但在查询字符串之前).
我的想法是,我将提取联盟会员ID,进行一些日志记录,然后在没有联盟会员ID的情况下对同一请求执行301.
例如:
http://www.domain.com/x32
http://www.domain.com/x32/
http://www.domain.com/path/to/something/x32
http://www.domain.com/x32?query=string
http://www.domain.com/x32/?query=string
http://www.domain.com/path/to/something/x32?query=string
http://www.domain.com/path/to/something/x32/?query=string
Run Code Online (Sandbox Code Playgroud)
我有这条路
routes.Add(new Route("{url}/x{affiliateExternalId}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(
new { controller = "Home", action = "LogCookieAndRedirect" }
),
Constraints = new RouteValueDictionary(new { affiliateExternalId = @"\d{1,6}" })
});
Run Code Online (Sandbox Code Playgroud)
哪个只匹配
http://www.domain.com/path/x32
http://www.domain.com/path/x32/
Run Code Online (Sandbox Code Playgroud)
我需要做什么来匹配所有内容并将查询字符串传递给控制器?有一个*我怀疑我应该使用的操作员,但是我不能让它做我需要的.
我正在使用MVC2 Preview 1开发一个非常简单的应用程序.
我有一个名为ContentController的控制器.我的问题是/ Content/Index工作正常,但/ Content /返回404.我在Studio Development Server上运行应用程序.
使用RouteDebugger测试但/ Content /返回404,并且不显示任何调试信息.
我没有更改路由代码:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
public class ContentController : Controller
{
IRepository _repo = new SimpleRepository("db", SimpleRepositoryOptions.RunMigrations);
public ActionResult Index()
{
var content = _repo.GetPaged<Content>(0, 20);
return View(content);
}
Run Code Online (Sandbox Code Playgroud) 如果用户使用http://www.mysite.com/Quote/Edit而不是http://www.mysite.com/Quote/Edit/1000访问我的网站会怎么样? 换句话说,他们没有为{指定值ID}.如果他们不这样做,我想显示一个漂亮的"未找到"页面,因为他们没有提供ID.我当前通过接受一个可以为null的int作为Controller Action中的参数来处理这个问题,它工作正常.但是,我很好奇是否有更标准的MVC框架处理方式,而不是我目前使用的代码(见下文).是一种更顺畅的方式来处理这个问题,还是这种方式很合适?
[HttpGet]
public ActionResult Edit(int? id)
{
if (id == null)
return View("QuoteNotFound");
int quoteId = (int)id;
var viewModel = new QuoteViewModel(this.UserId);
viewModel.LoadQuote(quoteId);
if (viewModel.QuoteNo > 0)
{
return View("Create", viewModel.Quote.Entity);
}
else
return View("QuoteNotFound");
}
Run Code Online (Sandbox Code Playgroud) 我的路由配置为需要一定的扩展名.
因此,HttpException当我尝试从visual studio调试时,我得到了一个:The incoming request does not match any route.
然后,我必须手动将该{controller}.{extension}部分附加到地址栏中的localhost URL.
我已经尝试过使用项目设置,但还没有设法让它工作.
所以,为了澄清,我希望我的项目直接进入
http://localhost:1440/mycontroller.ext
当我在本地机器上的visual studio下运行时.
c# asp.net asp.net-mvc asp.net-mvc-routing visual-studio-2008
我有一个ASP.Net应用程序,它有一个名为'Customers'的区域.该区域有一个名称相同的控制器,只有一个名为Index的方法.
我定义了以下路线:
context.MapRoute(null,
"Customers/{controller}/{action}",
new { controller = "customers", action = "Index" }
);
Run Code Online (Sandbox Code Playgroud)
这允许我导航以使用以下URL导航到我的Customers控制器上的索引方法.
MYDOMAIN /客户
在我的客户区域,我还有另一个叫做产品的控制器.这有许多方法可以让我使用产品实体(目前主要由Visual Studio自动生成).
使用我当前的路线,我可以使用以下URL链接到产品控制器:
MyDomain/Customers/Products(显示产品控制器的索引页面)MyDomain/Customers/Products/Create(显示添加新产品的页面).MyDomain/Customers/Products/Details?id = 1234(显示ID为1234的产品)
现在我想要做的是导航到详细信息页面,其中包含更加用户友好的URL,例如:
MYDOMAIN /客户/产品/ 1234
我已经定义了一个如下所示的新路由:
context.MapRoute(null,
"Customers/Products/{id}",
new { controller = "Products", action = "Details" }
);
Run Code Online (Sandbox Code Playgroud)
路线在我演示的第一条路线之前定义.这允许我根据需要导航到产品页面,但是我无法再导航到我的产品控制器上的其他方法.
EG以下URL
MYDOMAIN /客户/产品/创建
给我以下错误:
参数字典包含方法'System.Web.Mvc.ViewResult Details(Int32)'的非可空类型'System.Int32'的参数'id'的空条目
如果我更改路由的顺序,那么我可以导航到我的产品控制器上的所有方法,但我的详细信息URL将恢复为具有查询参数的旧格式.
如果我更新路线看起来像这样:
context.MapRoute(null,
"Customers/Products/{id}",
new { controller = "Products", action = "Details", id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
然后我仍然遇到同样的问题.
谁能告诉我如何构建我的路线以获得我想要的结果?综上所述:
我有一个2个控制器,其中一个是WebApi:
public class ListController : ApiController
{
public object Remove(string ListId, List<string> ItemIds)
{
//removed
}
}
public class ListController : Controller
{
public object Remove(string ListId, List<string> ItemIds)
{
//removed
}
}
Run Code Online (Sandbox Code Playgroud)
我的路线在Global.asax中注册如下:
WebApiConfig.Register(GlobalConfiguration.Configuration);
RouteConfig.RegisterRoutes(RouteTable.Routes);
Run Code Online (Sandbox Code Playgroud)
我的WebApi路由定义为:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
通过@ Url.Action("删除","列表")生成网址时,我得到的路径是'/ list/remove'.我期望选择WebApi路由('/ api/list/remove'),因为WebApi路由在其他路由之前注册.
如何让@ Url.Action按预期返回WebApi路由?
asp.net-mvc asp.net-mvc-routing asp.net-mvc-4 asp.net-web-api
我有两个动作,一个是带有此签名的HttpGet:
[Route("NewsLetter/SelectEmail/{page?}")]
[HttpGet]
public ActionResult SelectEmail(int? page, string priCat, string secCat)
{
...
}
Run Code Online (Sandbox Code Playgroud)
还有一个HttpPost有这个签名:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SelectEmail(int id)
{
...
}
Run Code Online (Sandbox Code Playgroud)
在为HttpGet方法设置上述路由之后,我注意到另一个方法HttpPost已停止工作,在挖掘之后我已经意识到路由HttpGet也为自己设置了HttpPost,直到我明确设置路由属性它才起作用为了它:
[Route("NewsLetter/SelectEmail/{id}")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SelectEmail(int id)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我想知道,这是一个错误吗?如果不是,那么无论如何设置路由属性[HttpGet]而不影响相应的[HttpPost]?
我正在尝试做这样的事情.
MyUrl.com/ComicBooks/{NameOfAComicBook}
我和RouteConfig.cs搞砸了,但我对此完全是新手,所以我遇到了麻烦. NameOfAComicBook是必需参数.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute("ComicBookRoute",
"{controller}/ComicBooks/{PermaLinkName}",
new { controller = "Home", action = "ShowComicBook" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
HomeController.cs
public ActionResult ShowComicBook(string PermaLinkName)
{
// i have a breakpoint here that I can't hit
return View();
}
Run Code Online (Sandbox Code Playgroud) 我有一个运行的MVC Web应用程序,www.domain.com我需要www.domain2.com为同一个Web应用程序的另一个域配置不同的URL绑定.
新域www.domain2.com必须返回特定的Controller Action View,如/Category/Cars:
routes.MapRoute(
name: "www.domain2.com",
url: "www.domain2.com",
defaults: new { controller = "Category", action = "Cars", id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
如何在不更改URL的情况下实现此目的,以便访问者插入URL www.domain2.com并接收视图,www.domain.com/category/cars但网址仍然存在www.domain2.com?
编辑:
我尝试过这种方法,但它不起作用:
routes.MapRoute(
"Catchdomain2",
"{www.domain2.com}",
new { controller = "Category", action = "Cars" }
);
Run Code Online (Sandbox Code Playgroud)