当你提出这个问题时,你会注意到问题的标题是在地址栏和你点击的链接到达这里.我不确定确切的术语如此难以搜索,但我怎么能做类似的事情呢?也就是说,如何将数据添加到纯粹用于显示/搜索引擎的地址栏.
谢谢
我正在从静态站点设计一个新的动态站点.我的路线全部排序但我对我的Action方法有疑问.
下面是代码,但是当测试并查看Firebug报告的标头时,如果我取出Response.End它是302重定向我假设因为我设置了301然后调用另一个动作使它成为302,但如果我放入Response.End我得到301.
我猜测添加Response.RedirectLocation实际上正在进行301重定向,所以我是否将我的返回值更改为EmptyResult或null,即使该行代码永远不会被执行,因此应用程序编译?
public ActionResult MoveOld(string id)
{
string pagename = String.Empty;
if(id == "2")
{
pagename = WebPage.SingleOrDefault(x => x.ID == 5).URL;
}
Response.StatusCode = 301;
Response.StatusDescription = "301 Moved Permanently";
Response.RedirectLocation = pagename;
Response.End();
return RedirectToAction("Details", new { pageName = pagename });
}
Run Code Online (Sandbox Code Playgroud) ASP.NET MVC 2 应用程序
我的控制器上有两个动作(Toons):
应用程序正在IIS7集成模式下运行,因此/ Toons/List工作正常.但是当我执行POST(内部重定向到/ Toons/List)时,它会重定向(使用302 Object Moved)返回/ Toons/Add.
如果我使用.aspx hack(在IIS6/IIS7经典模式下工作),问题就会消失.
但是如果没有.aspx - GET工作正常,但POST会将我重定向到自己,但使用GET.
我错过了什么?
我正在使用webhost4life.com托管,他们确实已经将IIS7更改为集成模式.
编辑:代码使用UltiDev Cassini服务器按预期工作.
编辑:事实证明,这是一个trash-in-URL问题.如果最后没有斜杠,IIS7会以某种方式正确路由请求.
EDET:行为的说明
当我请求(POST)/Toons/List(没有斜杠)时,IIS没有找到处理程序(我不知道IIS究竟是如何处理URL到处理程序的映射)并重定向请求(使用302代码)/Toons/List/(注意尾随斜杠).
根据HTTP规范,浏览器必须使用相同的方法(在这种情况下为POST)重定向请求,而是处理302,就好像它是303并发出对新URL的GET请求.
这是不正确的,但大多数浏览器的已知行为.
解决方案是使用.aspx-hack使IIS明确地将请求映射到ASP.NET处理程序,或者使用ASP.NET处理程序将IIS配置为处理虚拟目录中的所有内容.
问:有什么更好的方法来处理这个问题?
我正在尝试使用Maarten Balliauw的Domain Route类将子域映射到MVC2应用程序中的区域,以便我有以下URL:
http://admin.mydomain.com/home/index
Run Code Online (Sandbox Code Playgroud)
代替:
http://mydomain.com/admin/home/index
Run Code Online (Sandbox Code Playgroud)
到目前为止,我只取得了部分成功.执行被路由到正确区域中的正确控制器,但它无法找到正确的视图.我收到以下错误:
The view 'Index' or its master was not found. The following locations were searched:
~/Views/AdminHome/Index.aspx
~/Views/AdminHome/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
Run Code Online (Sandbox Code Playgroud)
这向我表明MVC只在根视图文件夹中查找视图,而不是在区域内查看视图文件夹.如果我将区域视图文件夹中的视图复制到根视图文件夹,页面呈现正常.然而,这完全违背了将APP划分为区域的目的.
我正在为该地区定义路线:
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "Admin"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.Add(
"Admin_Default"
, new DomainRoute(
"admin.localhost"
, "{controller}/{action}/{id}"
, new { controller = "AdminHome", action = "Index", id = UrlParameter.Optional }
));
}
}
Run Code Online (Sandbox Code Playgroud)
我很困惑为什么它找到区域内的控制器很好,但不是视图.
我有以下路线:
context.MapRoute
(
"PersonArea_tripleInt",
"PersonArea/{controller}/{action}/{int1}/{int2}/{int3}",
new { controller = "Person", action = "Index" }, new { int1 = new IntConstraint(), int2 = new IntConstraint(), int3 = new IntConstraint() }
);
Run Code Online (Sandbox Code Playgroud)
我在我的控制器中使用这样的:
RoleListUrl = Url.Action("RoleList", "Person", new { Area = "PersonArea" }),
Run Code Online (Sandbox Code Playgroud)
要生成此URL:"/ PersonArea/Person/RoleList"
在我看来,我有下拉列表,将填充int1,int2和int3值.我在客户端使用JavaScript来填写URL的其余部分:
"/ PersonArea /人/的RoleList/12 /76分之43"
这是第一次工作得很好,但是当页面重新加载时,在我的控制器中创建基本URL时会重新使用旧值(12,43,76).因此,当用户与下拉列表交互时,会在客户端上创建以下URL:
"/ PersonArea /人/的RoleList/12/43 /88分之76/78分之154"
如何让Url.Action在服务器的后续帖子中忽略旧值(12,43,76),以便客户端可以构造正确的URL?我正在使用硬编码控制器中的基本URL("/ PersonArea/Person/RoleList /").
我只希望控制器每次都生成"/ PersonArea/Person/RoleList",让客户端填写其余的参数.
我错过了什么?谢谢.
我试图找到登陆页面的最佳方式,我希望我的网址是这样的.
然后我想出了这个.
routes.MapRoute(
"Landing",
"",
new { controller = "Home", action = "Landing" }
);
routes.MapRoute(
"Home",
"Home",
new { controller = "Home", action = "Index" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
第一个是(Landing)是让www.myweb.com重定向到View/Home/Landing.cshtml.第二个(Home)是www.myweb.com/Home重定向到View/Home/Index.cshtml.
我想问有没有比这更好的方法?或者这很好吗?谢谢你的建议.
我有一个域名"http://www.abc.com".我在这个域上部署了一个ASP.net MVC4应用程序.我还在RouteConfig.cs中配置了默认路由,如下所示
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "MyApp", action = "Home", id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
以上映射确保任何试图访问"http://www.abc.com"的人自动显示"http://www.abc.com/MyApp/Home"页面
一切都按预期工作,但浏览器中的地址栏显示"http://www.abc.com"而不是"http://www.abc.com/MyApp/Home".有没有办法强制浏览器显示完整的URL,包括控制器和Action?
我正在接受MVC routing frame work.我对RouteCollection.MapRoute方法所采用的参数感到有点困惑.
如果我采取了以下请求的传统示例
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- List item
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
Run Code Online (Sandbox Code Playgroud)
它使用以下签名进行映射
public static Route MapRoute(
this RouteCollection routes,
string name,
string url,
Object defaults
)
Run Code Online (Sandbox Code Playgroud)
我的问题是关于"对象默认值",
new { controller = …我正在开发一个asp.net mvc应用程序.我正在为我的应用程序创建robots.txt以防止机器人,因为我当前的站点收到了很多机器人请求.所以我在MVC.NET 4中找到了这个链接,Robots.txt文件来创建robots.txt.但是当我访问我的应用程序时,如此输入网址"www.domain.com/robots.txt",它总是返回404页面.请参阅下面的代码.
这是我在HomeController中的动作方法
public ActionResult Robots()
{
Response.ContentType = "text/plain";
return View();
}
Run Code Online (Sandbox Code Playgroud)
这是我的机器人视图
@{
Layout = null;
}
User-agent:*
Disallow:/
Run Code Online (Sandbox Code Playgroud)
我在RouteConfig中为robots.txt配置了这样的路由
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapMvcAttributeRoutes();
routes.MapRoute(
"Robots.txt",
"robots.txt",
new { controller = "Home", action = "Robots" },
new string[] { "AyarDirectory.Web.Controllers" }
);
//other routes
}
Run Code Online (Sandbox Code Playgroud)
但是当我访问此网址"www.domain.com/robots.txt"时,它总是返回404页面.如何正确地将robots.txt添加到我的应用程序中?
我是MVC新手并编辑现有应用程序.目前我在RouteConfig.cs中看到以下内容:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Util",
"util/{action}",
new {controller = "util"});
routes.MapRoute(
"Catchall",
"{*url}",
new {controller = "Main", action = "CatchUrl"});
}
}
Run Code Online (Sandbox Code Playgroud)
在主控制器内部有逻辑,它基本上做a RedirectToRoute并设置调用的区域,控制器,动作和查询字符串location到某个值.
public class MainController : Controller
{
public ActionResult CatchUrl()
{
var agencyId = 9;
var routeValues = new RouteValueDictionary
{
{"area", "area1"},
{"controller", "dashboard"},
{"action", "index"},
{"location", "testLocation"}
};
return RedirectToRoute(routeValues );
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常,当你给它一个无效区域时,它正确地转到默认区域.
我还看到一个名为CustomAreaRegistration.cs的文件:
public abstract class CustomAreaRegistration : AreaRegistration
{
public …Run Code Online (Sandbox Code Playgroud)