简体   繁体   English

在MVC中,元标记内容url的替代方法是什么?

[英]What is an alternative in MVC to meta tag content url:?

I am working on an MVC project where I am using quite a bit of java script, I am trying to put the following code to my _Layout page: 我正在一个MVC项目中使用大量的Java脚本,正在尝试将以下代码放入_Layout页面:

<noscript>
    <meta http-equiv="refresh" content="0;url=/NoJScript" />
</noscript>

So I was following this tutorial step by step and the url=/NoJScript just doesn't take me to the controller method like it does for him when I shut off the java script in my browser. 因此,我一步一步地遵循了本教程 ,并且当我在浏览器中关闭Java脚本时,url = / NoJScript并没有带我去使用控制器方法。 I just get a 404. I put the view in its place as well of course. 我刚得到一个404。当然,我也将其放置在视图中。 I even tried putting the full path and still nothing. 我什至试图全力以赴,却一无所获。 I read somewhere that I shouldn't even use the URL under the meta tag because there is unreliable in some browsers. 我读到某个地方甚至都不应该使用meta标记下的URL,因为在某些浏览器中它不可靠。 So is there a code to automatically redirect me to my controller and action result method when java script is switched off in browser? 那么,在浏览器中关闭Java脚本时,是否有代码可以自动将我重定向到我的控制器和操作结果方法?

Here is the NoJScript controller : 这是NoJScript控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pansense.Controllers
{
public class NoJScriptController : Controller
{
    //
    // GET: /NoJScript/
    [AllowAnonymous]
    public ActionResult NoJavaScript()
    {
        return View("NoJavaScript");
    }
}
}

Here is the view called the same way as its method NoJavaScript: 这是一种与其方法NoJavaScript相同的视图:

@{
ViewBag.Title = "JavaScript required";
Layout = null; // <= Important!
}

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My MVC App</title>

@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")

</head>
<body>
To use this application JavaScript is required.
</body>
</html>

You need to name your action to Index like he does in the tutorial instead of NoJavaScript . 您需要像在本教程中一样将动作命名为Index ,而不是NoJavaScript Or you can change your url to /NoJScript/NoJavaScript 或者您可以将网址更改为/NoJScript/NoJavaScript

Further clarification: 进一步澄清:

Currently, your url in the meta tag will point to the default action on the NoJScriptController which, unless you changed your route values, will be the Index action. 当前,您在meta标记中的网址将指向NoJScriptController上的默认操作,除非您更改了路由值,否则它将为Index操作。 So you need to either specify the NoJavaScript action in the meta tag's url, or rename the action to Index . 因此,您需要在meta标签的url中指定NoJavaScript操作,或将操作重命名为Index

Note: if you rename the action, you also have to rename the View. 注意:如果重命名动作,则还必须重命名视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM