我怎样才能得到像localhost这样漂亮的网址:8888/News/Example-post而不是localhost:8888/Home/Details/2
我的HomeController具有以下Details方法
public ActionResult Details(int id)
{
var ArticleToView = (from m in _db.ArticleSet where m.storyId == id select m).First();
return View(ArticleToView);
Run Code Online (Sandbox Code Playgroud) 我不知道为什么我经常与此斗争,但有人可以解释为什么这不起作用?
/重定向到控制器的index动作home.
/gallery/ 抛出404未找到的错误.
/gallery/index重定向到控制器的index动作gallery.
从文档:
定义路径时,可以为参数指定默认值.如果URL中未包含该参数的值,则使用默认值.您可以通过将字典对象分配给Route类的Defaults属性来设置路由的默认值.
我不明白这是如何遵循这条规则的:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
它对我来说:
controller未定义a,请使用Home.action未定义,请使用Index.gallery并且URL中不包含操作,因此它应该转到Index.我是否遗漏了某些不必要的混淆和愚蠢的东西?
我总是发现MVC3路由有问题但接受了它.然后我开始使用Rails和Node框架,并且它们具有可笑的简单路由,所以现在.NET MVC只是在它不起作用时使我烦恼或者让我使用复杂的模式.
作为参考,以防有人询问,我的图库控制器,操作和视图都已定义并在我浏览时工作/gallery/index.
public class GalleryController : Controller
{
public ActionResult Index()
{
return View();
} …Run Code Online (Sandbox Code Playgroud) 我正在阅读 Pro Asp.net mvc3 框架书。我想更改默认路由,以便我可以拥有不同的主页。我添加了一个名为 Pages 的新控制器和一个名为 Home 的视图。这就是我想要的主页。
我试过将此添加到我的 global.asax.cs
routes.MapRoute("MyRoute", "{controller}/{action}/{id}",
new { controller = "Pages", action = "Home", id = "DefautId" });
Run Code Online (Sandbox Code Playgroud)
这会更改默认页面,但会破坏类别
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(null,
"", // Only matches the empty URL (i.e. /)
new
{
controller = "Product",
action = "List",
category = (string) null,
page = 1
}
);
routes.MapRoute(null,
"Page{page}", // Matches /Page2, /Page123, but not /PageXYZ
new {controller = "Product", action = "List", category = (string) null}, …Run Code Online (Sandbox Code Playgroud) 我想在以下网址上显示用户详细信息:
www.website.com/users/yasser
最后一个条目yasser是用户名我尝试了几条路线,但它确实没有用.
我的用户控制器如下所示.
public class UserController : Controller
{
public ActionResult Index(string username)
{
var model = _service.GetUserDetails(username);
return View(model);
}
}
Run Code Online (Sandbox Code Playgroud)
我已经提到了这个和其他几个链接,但我真的无法弄清楚它是如何工作的.
有人可以帮我解决这个问题.谢谢
编辑:
我当前的路线配置如下
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 }
);
}
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,我已将我的管理面板移动到区域,从那时起我无法点击adminpanel api调用.这是主api注册和管理面板api注册的代码.
//Api registration
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "Secondary",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//Area Registration
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_panel",
"AdminPanel/{controller}/{action}/{id}",
new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
context.Routes.MapHttpRoute(
"Admin_Secondary",
"AdminPanel/api/{controller}/{id}",
new { id = RouteParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
我试图用URL localhost/taw/adminpanel/api/SearchAPI来打它但它不起作用.我的路由有什么问题?
asp.net asp.net-mvc-routing asp.net-mvc-areas asp.net-web-api
名为"Home_default2"的路径已在路径集合中.路线名称必须是唯一的.
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "Default",
url: "area/{controller}/{action}/{id}",
defaults: new {area="Home_Default", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
public override string AreaName
{
get
{
return "Home";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default2",
"Home/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
自动生成的代码是错误的,我做错了什么?
我可能做的不对,因为我是这个框架的新手,希望你能帮忙^^
正如您在下面的代码中看到的那样,在每个操作中,我都会找到要更新的表,然后调用表中的方法。
public class TableController : Controller
{
private Lobby L;
public TableController()
{
L = Lobby.Instance;
}
public ActionResult Index(uint id)
{
Table T = L.Tables[id];
return View(T);
}
public ActionResult AddPlayer(byte pos, uint id)
{
Table T = L.Tables[id];
...
T.AddPlayer(p, pos);
...
}
...
Run Code Online (Sandbox Code Playgroud)
}
但是我注意到我在每个方法中都在做同样的事情,所以我虽然可以将表变成一个属性,这样我就不需要为每个动作找到它。
我想要这样的东西:
public class TableController : Controller
{
private Lobby L;
private Table T;
public TableController(uint tableId)
{
L = Lobby.Instance;
T = L.Tables[tableId];
}
public ActionResult Index()
{
return View(T);
} …Run Code Online (Sandbox Code Playgroud) 我可以毫无问题地从Windows应用程序访问我的Web API服务器.但是,从Android来看,却是另一回事
那为什么会这样?
"localhost"使用错误的东西(在URL中)?我应该使用计算机的名称吗?
字符串(字面意思为" http://localhost:28642/api/Departments/GetCount?serialNum=4242")是否需要逐字逐句?
调用REST方法的Windows应用程序和调用相同REST方法的Android应用程序之间的区别是什么会导致后者失败(连接被拒绝)?
android url-routing asp.net-mvc-routing asp.net-web-api econnrefused
我们的网站有一个REST API.我们一直在通过为端点添加端点添加或更新端点的发布版本来对端点进行版本控制.例如
这种方法的问题是我们的客户端代码调用了不同版本的端点.例如,页面可以调用/ v3/service-c和/ v1/service-a.
我想设置它,以便我们的开发人员可以通过在端点前添加最新版本的端点来访问最新版本的API.使用上面的示例,页面将调用/ v3/service-c和/ v3/service-a,对于service-a,请求将被转发到绑定到/ v1/service-a的操作,因为是/ v3之前的最新服务版本.
我知道我可以在代码中明确地手动添加路由,但是管理不同版本的端点会变得很困难.
我知道如何枚举路由表中的版本并解析它们; 这部分就解决了.但是,我不确定的是我究竟是如何截取路由的,以便可以将对/ v3/-service-a的调用转发到我为/ v1/service-a设置的路由.
有任何想法吗?
我为我的 asp net mvc 电子商务开发面包屑。我有一个用于我的类别的控制器。它看起来像这样:
public class CategoryController : AppController
{
public ActionResult Index(string cat1, string cat2, string cat3, int? page)
{
... some code
// build breadcrumbs from parent cats
int indexer = 0;
foreach(var item in parCategories) //parCategories - list of parent categories
{
string currCatIndex = new StringBuilder().AppendFormat("category{0}", indexer + 1).ToString(); //+2 cause arr index begins from 0
var currNode = SiteMaps.Current.FindSiteMapNodeFromKey(currCatIndex);
currNode.Title= parCategories.ElementAt(indexer).Name;
indexer++;
}
string finalCatIndex = new StringBuilder().AppendFormat("category{0}", CategoryDepth + 1).ToString();
var node = …Run Code Online (Sandbox Code Playgroud)