我想为我的博客应用程序创建一个RESTful API,但我不知道如何映射这样的控制器.
怎么做到呢?Post应该在URL模式中进行硬编码吗?
如何使用razor视图引擎直接访问ASP.NET MVC中的.cshtml文件?
例如,我有这个网址:localhost/Home/About.这将在"主"页面内加载about网站.
我想加载about页面而不加载母版页.所以我在想我可以使用这个url:localhost/Home/About.cshtml.但它不起作用.
如何在不加载母版页的情况下加载视图页面?
有可能以某种方式将所有视图路由到一个特定视图吗?我希望有一个"Under Construction view",它始终是默认视图,直到我"翻转开关"而不必构建.在此之前,所有其他控制器操作都会路由到此视图
我很想知道我是否可以在web.config中完成它,或者如果我必须在Global.asax中的RegisterRoutes中有一些if/else.
我究竟做错了什么?
我的默认/用户路线
routes.MapRoute(
"User", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {controller = "User", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)
我想分开代码,所以我创建了另一个控制器"UserProducts"
我的路线
routes.MapRoute(
"UserProducts", // Route name
"user/products/{action}/{id}", // URL with parameters
new { controller = "UserProducts", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)
我的UserProducts控制器中有ActionResult索引,但是我的
localhost/user/products
Run Code Online (Sandbox Code Playgroud)
不起作用:
Error 404 - The resource cannot be found.
Run Code Online (Sandbox Code Playgroud) 我知道你可以通过网址传递参数,.com/MyPage/?controlID=5但你怎么能用类似的东西来做.com/MyPage/5?因此不需要变量名称或问号.
我想在MVC中制作几个相同的地图路线.
localhost:1010/abcd/home/index
localhost:1010/home/index/abcd
id = abcd controller = home action = index
我使用波纹管代码,但它不起作用
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"ShoppingManagment",
"{id}/{controller}/{action}",
new { controller = "ShoppingManagment",
action = "ShoppingManagment", id = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index", id = UrlParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud) 我们有一个像这样的路由配置:
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "About",
url: "{controller}/{action}/{aboutId}",
defaults: new { controller = "Home", action = "About" }
);
routes.MapRoute(
name: "Contact",
url: "{controller}/{action}/{contactId}",
defaults: new { controller = "Home", action = "Contact" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
您会注意到有两个路由具有一个必需的额外参数.路线About和Contact.
在我们的应用程序中,我们有两个网址
www.myapp.com/Home/About/2哪个工作正常.但是当我们浏览浏览器时,www.myapp.com/Home/Contact/5我们会遇到可怕的路由异常:
The parameters dictionary contains a null entry for parameter 'contactId' …Run Code Online (Sandbox Code Playgroud) 抛出以下异常Url.Link(...):
An exception of type 'System.ArgumentException' occurred in System.Web.dll but was not handled in user code
Additional information: A route named 'api/companies/{companyId:Guid}' could not be found in the route collection.
Run Code Online (Sandbox Code Playgroud)
这是控制器方法:
[Route("api/companies/")]
[HttpPost]
public IHttpActionResult Create()
{
Company company = new Company("Test!");
CompanyRepository.Add(company);
return Created(new Uri(Url.Link("api/companies/{companyId:Guid}", new { companyId = company.Id })), company);
}
Run Code Online (Sandbox Code Playgroud)
此处使用属性路由定义路由:
[Route("api/companies/{companyId:Guid}")]
[HttpGet]
public IHttpActionResult Get(Guid companyId)
{
return Ok(CompanyRepository.Find(companyId));
}
Run Code Online (Sandbox Code Playgroud)
为什么无法找到路线? 我需要在传统路由中使用RouteConfig吗?
我有一个ASP.NET MVC 5控制器使用属性路由,似乎没有使用DELETE操作.
[Route("deletealbum/{id}")]
[AcceptVerbs(HttpVerbs.Delete)]
public ActionResult DeleteAlbum(int id)
{
// rest of the code omitted here
return Json(true, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
我明确地将路由名称更改为deletename以排除其他路由过载冲突.
上面的操作没有触发 - 我从IIS中找不到404.但是,如果我将上面的动作切换到HttpVerbs.Get路线是没有问题的.
长期以来第一次使用MVC而不是Web API构建API,我无法弄清楚为什么路由没有触发 - 它几乎看起来像任何DELETE操作都被阻止了.我在不同的控制器中看到相同的行为 - GET工作正常,但是DELETE操作返回404.是否需要翻转某些配置设置以启用其他动词?
我想在我的控制器中有两个方法具有相同的路由,但仅在HTTP方法上有所不同.具体来说,如果我的路线看起来像
routes.MapRoute(
name: "DataRoute",
url: "Sample/{user}/{id}/",
defaults: new { controller = "Sample", action = "--not sure--", user = "", id = "" }
);
Run Code Online (Sandbox Code Playgroud)
我的控制器中有2个方法:
[HttpGet]
public void ViewData(string user, string id)
[HttpPost]
public void SetData(string user, string id)
Run Code Online (Sandbox Code Playgroud)
ViewData()如果我GET 则调用所需的行为Sample/a/b,SetData()如果我POST Sample/a/b,则调用相同的URL.
我知道我可以创建两条不同的路线,但出于设计原因,我希望只有一条路线可以区分GET和POST.有没有办法配置路由或控制器来执行此操作而无需创建新路由?