我试图以文件/ id格式获取我的URL .我猜我的控制器中应该有两个Index方法,一个带参数,一个带不带.但我在下面的浏览器中收到此错误消息.
无论如何这里是我的控制器方法:
public ActionResult Index()
{
return Content("Index ");
}
public ActionResult Index(int id)
{
File file = fileRepository.GetFile(id);
if (file == null) return Content("Not Found");
else return Content(file.FileID.ToString());
}
Run Code Online (Sandbox Code Playgroud)
更新:完成添加路线.谢谢Jeff
c# model-view-controller asp.net-mvc controller asp.net-mvc-routing
我正在学习如何在ASP.NET MVC中创建自定义路由并且遇到了障碍.在我的Global.asax.cs文件中,我添加了以下内容:
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
);
// My Custom Route.
routes.MapRoute(
"User_Filter",
"home/filter/{name}",
new { controller = "Home", action = "Filter", name = String.Empty }
);
}
Run Code Online (Sandbox Code Playgroud)
我的想法是能够导航到http://localhost:123/home/filter/mynameparam.这是我的控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Filter(string name)
{
return this.Content(String.Format("You found …Run Code Online (Sandbox Code Playgroud) 我有一个自定义模型绑定器,用于检查用户是否可以访问他要求的文档; 我想知道如何测试使用此自定义绑定器的路径?
我尝试这个测试,但我收到此错误:
MvcContrib.TestHelper.AssertionException:参数'contract'的值不匹配:预期'Domain.Models.Contract'但是''; 在名为'contract'的路由上下文操作参数中找不到任何值 - 您的匹配路由是否包含名为'contract'的令牌?
[SetUp]
public void Setup()
{
MvcApplication.RegisterModelBinders();
MvcApplication.RegisterRoutes(RouteTable.Routes);
}
[Test]
public void VersionEdit()
{
var contract = TestHelper.CreateContract();
var route = "~/Contract/" + contract.Id.ToString() + "/Version/Edit/" +
contract.Versions.Count;
route.ShouldMapTo<VersionController>(c => c.Edit(contract));
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试调试自定义绑定器永远不会被调用.
我的路线定义:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"VersionToken", // Route name
"Contract/{contractId}/Version/{version}/{action}/{token}", // URL with parameters
new { controller = "Version", action = "ViewContract", version = 1, token = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Version", // Route …Run Code Online (Sandbox Code Playgroud) 如果我有以下网址:
/ someurl
我有两个域名:
us.foo.com
au.foo.com
我希望这个200(匹配):
us.foo.com/someurl
但这到404(不匹配):
au.foo.com/someurl
路线看起来像这样:
RouteTable.Routes.MapRoute(
"xyz route",
"someurl",
new { controller = "X", action = "Y" }
);
Run Code Online (Sandbox Code Playgroud)
我猜是因为没有路由值,我不能基于主机限制URL?那是对的吗?
如果是这样,我怎么能这样做,除了动作中的以下(丑陋):
if (cantViewThisUrlInThisDomain)
return new HttpNotFoundResult();
Run Code Online (Sandbox Code Playgroud)
有人有任何想法吗?
我想我有点想通过它的域来限制路由,而不是路由令牌,如果这是有道理的.
asp.net-mvc domain-name asp.net-mvc-routing route-constraint asp.net-mvc-3
我在一个区域有一条自定义路线如下:
context.Routes.Add(
"SearchIndex - By Location - USA",
new CountryTypeSpecificRoute(
CountryType.UnitedStates,
"search/{locationType}-in-{query}",
new { controller = "Search", action = "Index", query = UrlParameter.Optional },
new { locationType = new UsaLocationSearchRouteConstraint() })
);
Run Code Online (Sandbox Code Playgroud)
示例网址:
/搜索/社区功能于新约克城
解决行动很好.但它无法找到View.
未找到视图"索引"或其主数据或视图引擎不支持搜索的位置.搜索了以下位置:〜/ Views/Search/Index.cshtml~/Views/Shared/Index.cshtml
该视图位于〜/ Areas/Search/Views/Search/Index.cshtml中
为什么不看那里?
如果我这样做context.MapRoute而不是context.Routes.Add,那就有效.所以它似乎与我使用自定义路线的事实有关?
有任何想法吗?
asp.net-mvc asp.net-mvc-routing asp.net-mvc-areas asp.net-mvc-3
我有一个页面,我想要包含一个"主页"链接,它将我带到我的应用程序的基本URL.到目前为止,我设法实现这一目标的最简单方法是通过以下Razor代码行,但它并不漂亮,我对此并不十分自信:
@Html.RouteLink(MyResources.HomeLinkLabel, new { controller = "" })
Run Code Online (Sandbox Code Playgroud)
请注意,如果我不包含,controller = ""那么它生成的超链接将我带到当前页面,而不是我的基本URL.
我觉得我错过了一些明显的东西......这样做的正确方法是什么?
http://localhost:49397/ChildCare/SponsorChild/83
这是链接,当我点击表格中的动作链接并重定向到编辑动作时生成链接,现在我想隐藏URL中的数字'83'我该如何实现这一点,
我正在使用VS2010 MVc4 Razor,对不起,我提前做了一个糟糕的英特尔谢谢
似乎有一千人在堆栈溢出时询问相同的问题,但似乎没有单一的解决方案来解决这个问题.我要再问一次......
我有一个API控制器,它具有以下操作:
// GET api/Exploitation
public HttpResponseMessage Get() {
var items = _exploitationRepository.FindAll();
var mappedItems = Mapper.Map<IEnumerable<Exploitation>, IEnumerable<ExploitationView>>(items);
var response = Request.CreateResponse<IEnumerable<ExploitationView>>(HttpStatusCode.OK, mappedItems);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { }));
return response;
}
// GET api/Exploitation/5
[HttpGet, ActionName("Get")]
public HttpResponseMessage Get(int id) {
var item = _exploitationRepository.FindById(id);
var mappedItem = Mapper.Map<Exploitation, ExploitationView>(item);
var response = Request.CreateResponse<ExploitationView>(HttpStatusCode.OK, mappedItem);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = id }));
return response;
}
// GET api/Exploitation/GetBySongwriterId/5
[HttpGet, ActionName("GetBySongwriterId")]
public HttpResponseMessage GetBySongwriterId(int id) { …Run Code Online (Sandbox Code Playgroud) 我想创建简单的博客引擎.对于花哨而干净的url,我想使用在MVC4中实现的路由机制.
我在RouteConfig.cs中添加了以下行:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ArticleList",
url: "Articles/{category}/{page}",
defaults: new
{
controller = "Articles",
category = UrlParameter.Optional,
page = 1
});
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在网页浏览器中写道:
http://localhost:6666/Articles/SomeCategory/3
Run Code Online (Sandbox Code Playgroud)
我想转移到这个控制器:
public class ArticlesController : ControllerBase<IHomeService>
{
public ActionResult Index(string category, int page = 0)
{
return View("~/Views/Article/Articles.cshtml");
}
}
Run Code Online (Sandbox Code Playgroud)
参数category ="SomeCategory"和page = 1. …
尝试访问我的一个Web API操作时,我收到404未找到错误:
我有我的WebAPIConfig:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
我有一个名为OnSourceAPIController的控制器和一个GET操作ValidateServiceRequestAuto.
我尝试去的时候收到404错误:
http://localhost/api/OnSourceAPI/ValidateServiceRequestAuto
Run Code Online (Sandbox Code Playgroud)
我的第一个猜测是我遇到了某种路由错误,但我为它创建了一个mvc控制器和一个索引页,并且能够通过这种路由(api/controller/action)正常浏览.内置的api/help页面甚至将/ api/OnSourceAPI/ValidateServiceRequestAuto显示为我的api调用的路由.
我也尝试过使用WebAPIRouteDebugger工具.当我使用它时,它给我一个000 404找不到的错误
http://localhost/api/OnSourceAPI/ValidateServiceRequestAuto
Run Code Online (Sandbox Code Playgroud)
,但实际上给我正常的路线信息,如果我把localhost从前面拿走并且只是使用
api/OnSourceAPI/ValidateServiceRequestAuto as the URL...
Run Code Online (Sandbox Code Playgroud)
任何指针将不胜感激.