如何从浏览器中屏蔽控制器和动作名称

如何从浏览器中屏蔽控制器和动作名称

本文介绍了如何从浏览器中屏蔽控制器和动作名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在浏览器中显示主机网址.

示例:

仅显示:

在浏览器中

解决方案

首先,就可用性和SEO而言,所谓的良好内容"是 用户可以链接的内容 .

如果您构建没有URL路径的页面",则您的用户将无法链接到该页面,共享该链接等.搜索引擎也将无法对其进行爬网,因此它将无法对其进行爬网.得到索引.

在某些情况下(安全问题),可能需要此行为以防止用户之间共享资源.

标准方式

在这种情况下,最好的选择是借助JavaScript框架(例如 AngularJS (请参阅此教程来开始).与服务器进行交互(交换数据)的最佳选择是使用 WebApi ,但任何导航"完全在浏览器中进行.

非标准方式

另一个选择是在MVC中自定义路由,以便它可以从请求的另一部分读取导航,并提出一些自定义约定,该约定确定从请求生成的路由值,以便MVC知道要调用哪个控制器动作.您可以使用自定义RouteBase 实现. >

这里是一个例子:

查看

@using (Html.BeginForm())
{
    <input type="hidden" name="location-controller" value="Home" />
    <input type="hidden" name="location-action" value="Index" />

    <input type="submit" value="Home" />
}

@using (Html.BeginForm())
{
    <input type="hidden" name="location-controller" value="Home" />
    <input type="hidden" name="location-action" value="Contact" />

    <input type="submit" value="Contact" />
}

@using (Html.BeginForm())
{
    <input type="hidden" name="location-controller" value="Home" />
    <input type="hidden" name="location-action" value="About" />

    <input type="submit" value="About" />
}

PathlessRoute

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication12
{
    public class PathlessRoute : RouteBase
    {
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            RouteData result = null;
            var form = httpContext.Request.Form;

            // Skip any forms that are not part of our scheme
            if (form != null && form.HasKeys())
            {
                var controller = form["location-controller"];
                var action = form["location-action"];

                if (!string.IsNullOrWhiteSpace(action))
                {
                    // Default controller to "Home"
                    if (string.IsNullOrWhiteSpace(controller))
                    {
                        controller = "Home";
                    }

                    result = new RouteData(this, new MvcRouteHandler());
                    result.Values["controller"] = controller;
                    result.Values["action"] = action;

                    // TODO: Work out scheme to pass custom route values (such as "id")
                }
            }

            return result;
        }

        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            return null;
        }
    }
}

用法

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.Add("Pathless", new PathlessRoute());

        routes.MapRoute(
            name: "Home",
            url: "",
            defaults: new { controller = "Home", action = "Index" }
        );
    }
}

现在,当您单击联系人"提交按钮时,您将获得联系人视图,但是URL中没有指示您正在查看哪个页面的路径.


您可以通过创建自己的 HTML帮助程序扩展方法以呈现表单标签的方式与使用ActionLink创建超链接的方式相同.

@Html.ActionLinkPost(name: "Contact", action: "Contact", controller: "Home")

您甚至可以将按钮设置为看起来像超链接的样式,或者使用一些JavaScript制作发布的真实超链接.


但是,如果您走这条路,您将在新水域中游泳,并且违反了标准的HTTP约定(例如,您正在使用POST(这是非标准的)进行导航).您可以确保不会在网络上找到太多(如果有的话)支持来克服可能遇到的任何陷阱.

I want to display just the host url in the browser.

Example:

to only display:

in browser

解决方案

First of all in terms of usability and in terms of SEO, what is considered "good content" is something that your users can link to.

If you build a "page" that has no URL path, then your users won't be able to link to it, share the link, etc. Search engines will also not be able to crawl it, so it will not get indexed.

There are some situations (security concerns) where this behavior may be desirable to prevent a resource from being shared between users.

The Standard Way

In that case, your best option would be to create a Single Page Application with the help of a JavaScript framework such as AngularJS (see this tutorial to get started). Your best option for interaction (exchanging data) with the server would be to use WebApi, but any "navigation" happens entirely in the browser.

The Non-Standard Way

Another option is to customize the routing in MVC so it can read the navigation from another part of the request and come up with some custom convention that determines what route values to generate from the request so MVC knows which controller action to call. You could do that with a custom RouteBase implementation.

Here is an example:

View

@using (Html.BeginForm())
{
    <input type="hidden" name="location-controller" value="Home" />
    <input type="hidden" name="location-action" value="Index" />

    <input type="submit" value="Home" />
}

@using (Html.BeginForm())
{
    <input type="hidden" name="location-controller" value="Home" />
    <input type="hidden" name="location-action" value="Contact" />

    <input type="submit" value="Contact" />
}

@using (Html.BeginForm())
{
    <input type="hidden" name="location-controller" value="Home" />
    <input type="hidden" name="location-action" value="About" />

    <input type="submit" value="About" />
}

PathlessRoute

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication12
{
    public class PathlessRoute : RouteBase
    {
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            RouteData result = null;
            var form = httpContext.Request.Form;

            // Skip any forms that are not part of our scheme
            if (form != null && form.HasKeys())
            {
                var controller = form["location-controller"];
                var action = form["location-action"];

                if (!string.IsNullOrWhiteSpace(action))
                {
                    // Default controller to "Home"
                    if (string.IsNullOrWhiteSpace(controller))
                    {
                        controller = "Home";
                    }

                    result = new RouteData(this, new MvcRouteHandler());
                    result.Values["controller"] = controller;
                    result.Values["action"] = action;

                    // TODO: Work out scheme to pass custom route values (such as "id")
                }
            }

            return result;
        }

        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            return null;
        }
    }
}

Usage

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.Add("Pathless", new PathlessRoute());

        routes.MapRoute(
            name: "Home",
            url: "",
            defaults: new { controller = "Home", action = "Index" }
        );
    }
}

Now when you click on the "Contact" submit button you will get the contact view, but the URL will not have a path indicating which page you are looking at.


You could improve things more by creating your own HTML helper extension method to render your form tags the same way you can use ActionLink to create hyperlinks.

@Html.ActionLinkPost(name: "Contact", action: "Contact", controller: "Home")

You could even style the buttons to look like hyperlinks or use some JavaScript to make real hyperlinks that post.


But if you go down this road, you are swimming in new waters and going against standard HTTP conventions (for example, you are navigating with POST, which is not standard). You can be sure you won't find much (if any) support on this approach around the web to overcome any pitfalls that you may run into.

这篇关于如何从浏览器中屏蔽控制器和动作名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:15