我有一个奇怪的问题,其中ViewContext.RouteData.Values ["action"]在我的登台服务器上为空,但在我的开发机器(asp.net开发服务器)上工作正常.
代码很简单:
public string CheckActiveClass(string actionName)
{
string text = "";
if (ViewContext.RouteData.Values["action"].ToString() == actionName)
{
text = "selected";
}
return text;
}
Run Code Online (Sandbox Code Playgroud)
我在ViewContext.RouteData.Values ["action"]行上收到错误.错误是:
异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例.
任何帮助表示赞赏.提前致谢.
如何使用我在Global.asax中定义的路由获取相对Url的控制器名称?
例:
如果我有这样的路线违规:
routes.MapRoute(
"Default", // Route name
"{language}/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "", language = "en" }
Run Code Online (Sandbox Code Playgroud)
从字符串"〜/ en/products/list"我想要产品(控制器名称).有没有现成的方法已经这样做了?
我在global.asax的底部有以下路由:
//404 ERRORS:
routes.MapRoute(
"404-PageNotFound",
"{*url}",
new { controller = "Error", action = "PageNotFound" }
);
Run Code Online (Sandbox Code Playgroud)
哪个在Visual Studio中工作正常,但在生产中我得到了IIS错误页面.
这条路线不应该捕获任何未被其他人捕获的URL,因此从IIS的角度来看没有404吗?我还需要在web.config中做些什么吗?
注:我不希望重定向到一个404特定URL; 而是我在请求的URL上提供404错误页面(我认为从可用性的角度来看这是正确的方法).
更新
在我的错误控制器中,我正在设置Response.StatusCode = 404;,这似乎是问题.当我删除它并再次部署到生产时,我再次获得友好的错误页面.但是,我相信我确实需要HTTP标头中的404状态 - 出于搜索引擎优化的目的 - 所以现在我的问题变为:
修订问题
IIS如何/为什么拦截响应并发送其开箱即用的404错误,我该如何防止这种情况?
**解决方案**
Dommer获得奖品以表明Response.TrySkipIisCustomErrors=true;哪些(我认为)是必要的.但还有另外两个关键细节:
让它在任何地方工作
由于某些URL可能会映射到"404-PageNotFound"以外的路由但包含无效参数,并且由于我不想重定向到404页面,因此我在基本控制器中创建了此操作:
[HandleError]
public ActionResult NotFound()
{
Response.StatusCode = 404;
Response.TrySkipIisCustomErrors = true;
return View("PageNotFound", SearchUtilities.GetPageNotFoundModel(HttpContext.Request.RawUrl));
}
Run Code Online (Sandbox Code Playgroud)
并且在任何继承基础的控制器动作中,每当我捕获无效的路由参数时,我只需要调用它:
return NotFound();
Run Code Online (Sandbox Code Playgroud)
注意:不是 RedirectToAction()
锦上添花:
我生成并传递到视图中的模型是将URL的多余部分输入到我们的搜索引擎中,并在友好的404页面上显示前3个结果作为建议.
有没有办法将参数传递给控制器而不将其放在URL上?
例如, http://www.winepassionate.com/p/19/wine-chianti-docg-la-moto
URL上的值为19.如果您实际将该值更改为另一个,则即使页面名称保持不变,页面也会显示不同的记录.
所以我想不传递URL上的ID,但仍然可以将其传递给Controller.建议的方法是什么?
在我的ASP.NET MVC 4应用程序的RouteConfig文件中,我已注册以下默认路由:
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "home", action = "index", id = UrlParameter.Optional });
Run Code Online (Sandbox Code Playgroud)
现在,在我的Razor视图中,我想生成一个指向我的应用程序根目录的URL:
<a href="@Url.Action("index", "home")">Home</a>
Run Code Online (Sandbox Code Playgroud)
生成的URL包括尾部斜杠; 单击该链接将打开localhost/IISApplicationName/页面.但是,我希望URL不包含尾部斜杠,以便URL是localhost/IISApplicationName.为其他操作生成路由(例如/ Account/Login)不会创建带有斜杠的URL - 它只是链接到应用程序根目录的路径.
有没有办法阻止ASP.NET MVC路由将尾部斜杠附加到上面的路由?
(我知道我可以从包含斜杠的URL重定向到没有斜杠的URL,但我宁愿让路由生成正确的路由URL.)
要继续我之前的一个问题(生成传出URL时选择了意外的路由),请考虑以下路由(没有默认值,没有约束):
"{controller}/{month}-{year}/{action}/{user}"
Run Code Online (Sandbox Code Playgroud)
假设在通过匹配此路由的传入URL呈现某些页面时(因此请求上下文包含每个路径段的值),我需要生成仅更改的URL,month并year保留所有其他段的确切值.
根据规则,我在链接的问题中提到过(即,路由系统将仅重用URL模式中早先出现的段变量的值,而不是任何提供的参数.),如果我指定new month并且year只通过匿名对象,我将失去该user段的价值(即路线甚至不会匹配).
我知道有两种方法可以解决这个问题:
根本不指定匿名对象; 但是在context.RouteData.Values;中设置必要的段值; 在生成所需的url之后,返回原始值,以便使用原始请求段值执行其余页面呈现; 这看起来像这样:
public static MvcHtmlString GetDateUrl(this RequestContext context,
DateTime date)
{
UrlHelper url = new UrlHelper(context);
object month = null, year = null;
if (context.RouteData.Values.ContainsKey("month"))
{
month = context.RouteData.Values["month"];
year = context.RouteData.Values["year"];
}
try
{
context.RouteData.Values["month"] = date.Month;
context.RouteData.Values["year"] = date.Year;
return MvcHtmlString.Create(
url.Action((string)context.RouteData.Values["action"]));
}
finally
{
if (month == null)
{
if (context.RouteData.Values.ContainsKey("month"))
{
context.RouteData.Values.Remove("month");
context.RouteData.Values.Remove("year"); …Run Code Online (Sandbox Code Playgroud)我有这条路线:
我的网站路线WebSite/Global.asax.cs:
namespace WebSite
{
public class MvcApplication : HttpApplication
{
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
...
routes.MapRoute(
"Default",
"Authenticated/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "WebSite.Controllers" }
);
...
}
void Application_Start()
{
...
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的管理区路线WebSite/Areas/Admin/AdminAreaRegistration.cs:
namespace WebSite.Areas.Admin
{
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext …Run Code Online (Sandbox Code Playgroud) asp.net-mvc asp.net-mvc-routing asp.net-mvc-areas asp.net-mvc-3
我正在尝试在AngularJS中构建单页面应用程序,并在HTML5模式下完成客户端路由.如果用户曾在我的网站上为页面添加书签,我理解我需要一个基本的服务器端路由方案,但我真的希望它只是总是提供我的单个主页并将相同的URL传播到客户端路由部分.
这个包罗万象的路线完美适用于只有一个深度的简单URL:
routes.MapRoute(
"Default", // Route name
"{*catchall}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)
示例网址:
http://myapp.com/moreInfo
http://myapp.com/contactUs
Run Code Online (Sandbox Code Playgroud)
"moreInfo"和"contactUs"只是Angular中的命名路由.单页应用程序的唯一真正入口点是http://myapp.com.到目前为止,这一切都非常顺利.
但是,当我尝试深入到多个路径时,应用程序会进入无限循环:
http://myapp.com/user/5
Run Code Online (Sandbox Code Playgroud)
这是因为我的服务器端路由不足以将"user/5"URL"传递"到我的客户端应用程序吗?我是否需要做一些特别的事情以确保无论URL的嵌套程度如何,它都会正确地传递给我的SPA?
我们有一个MVC 5.1项目,正在使用属性路由.一切都工作正常,除了默认页面上有登录表单.
[RoutePrefix("Home")]
public class HomeController : BaseController
{
[Route("~/")]
[Route]
[Route("Index")]
[HttpGet]
public ActionResult Index()
{
var model = new LoginViewModel();
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(String Username, String Password)
Run Code Online (Sandbox Code Playgroud)
表格通过GET罚款显示,但在POST后我们得到......
HTTP错误405.0 - 不允许的方法
由于使用了无效的方法(HTTP动词),因此无法显示您要查找的页面.
通常,默认路由将处理POST和GET罚款.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{dealerId}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
显然我在默认路由上的帖子的路由中遗漏了一些东西,因为其他页面上的后续帖子工作正常.
有没有人这样做过?
谢谢,
c# asp.net-mvc-routing attributerouting asp.net-mvc-5 asp.net-mvc-5.1
这是场景的方式:
TestController的Index方法TestControllerIndexAction方法TestController:
[Authorize]
public class TestController : Controller
{
// GET: Test
public ViewResult Index()
{
return View();
}
[ValidateInput(false)]
public ActionResult ActionTest()
{
return new EmptyResult();
}
}
Run Code Online (Sandbox Code Playgroud)
HomeController:
[Authorize]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
AccountController:
public class AccountController : Controller
{
[AllowAnonymous]
public ActionResult Login()
{
return View(); …Run Code Online (Sandbox Code Playgroud) security authentication asp.net-mvc forms-authentication asp.net-mvc-routing