create a random URL with each Instance - asp.net

I'm working on the security part of one of the upcoming websites developed based on ASP mvc. Here the target is like below.
I enter say localhost:12345 I get redirected to localhost:1234/Login, But my requirement is as below.
Every time an user enters localhost:12345, it should create a random number and it should be shown to user as localhost:12345/{hereIsTheRandomAlphaNumericStringGenerated}, for example say user 1 is logged in, he is having the url as localhost:12345/user!Rand123 and there is another user logged in and his URL should be localhost:12345/a12fer43r5. if someone enters this randomly generated URL to their web page, it should give an error. The base underying URL should be the same Login, but the names should be generated randomly. And also, if the user enters localhost:12345/login even in this case, the user should not be permitted.
in my RouteConfig.cs I've the below code available.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace ComplianceBot
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Login",
id = UrlParameter.Optional
}
);
}
}
}
Please let me know how can I do this.

Related

Trying to display Custom View in MVC app but it says requested URL not found

I am new to azure, MVC and also ASP.NET. I am writing MVC Cloud service with ASP.NET web role. Please help me with this problem
When I create the application there are default views but I wanted to see my view so I set my view as start page. I also changed the values in RegisterRoutes method
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "User", action = "AddUser", id = UrlParameter.Optional }
);
}
When I run the app, it gives HTTP 404 error because it could not find request URL : /Views/User/AddUser.cshtml
In MVC you don't put the view in the URL to get it rendered.
This won't work: /Views/User/AddUser.cshtml
As you've correctly put in your question the default route is {controller}/{action}/{id} with id being optional.
So assuming that User is your controller, i.e. you have a class called UserController, which looks something like:
namespace My.Controllers
{
public class UserController : Controller
{
which has an action on it called AddUser:
public ActionResult AddUser()
{
// implementation logic
return View();
}
Then the default route will display your view when it processes the URL /User/AddUser
In MVC 5, this looks something like:

Running an action method on every view

I want to show the version info of the current project on every page.
The way I do it in the moment is like this:
Inside the Index method of my homecontroller:
ViewBag.Version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
Inside the _layout.cshtml(masterpage):
#ViewBag.Version
The Problem here is, it will only displayed once, but I want to display the Version on every page/view.
This is my routing configuration:
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 }
);
}
Do I have to change something here ?
Ty for helping
Why not just call
#System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
on the _Layout.cshtml view?
MVC has a different launch context and a work around is needed to text the version:
#typeof(HomeController).Assembly.GetName().Version
HomeController could be replaced with any other type in the assembly the MVC application assembly. See this question for more info.
Just put this in the Layout.
<label>#System.Reflection.Assembly.GetExecutingAssembly().GetName().Version</label>

Pluralsight 404 not found error

I have a question about MVC3 in the Pluralsight examples. I'm new to MVC and I have what will appear to be a simple question. I downloaded the sample code and added the Routemap to global.asax.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace OdeToFood
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Cuisine",
"cuisine/{name}",
new { controller = "cuisine", action = "Search" }
);
/* routes.MapRoute(
"Cuisine",
"{controller}/{name}",
new { controller = "cuisine", action = "Search" }
); */
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}
And added the Controller:
namespace OdeToFood.Controllers
{
public class CuisineController : Controller
{
//
// GET: /Cuisine/
public ActionResult Search()
{
return Content("You have reached the Cuisine controller");
}
}
}
As shown in the tutorial "Controller Action Parameter!" and run the application with the word cuisine (all spelled correctly - even changing to all capitalization as a test) and I still get the HTTP 404 "not found error".
I'm running on Windows 7 with VS 2012 and .net 4.5 installed (this is a new box and may not have ever had previous versions. MVC 3 and MVC 4 are in the new project selection so those must be isntalled correclty.
Any ideas on what I'm doing wrong? Did I miss a step? I see that IIS6 or IIS7 might/must be on the machine? I have come to believe that IIS doesn't run on windows 7. Is that true? Do I require iis? The sample code works fine until this change...
I'm a little over my head here as I learn this new stuff. Thank you for your patience and help!
Try using:
routes.MapRoute(
"Cuisine",
"cuisine/{name}",
new { controller = "cuisine", action = "Search", name = "" }
);
Try using the a MVC Route Debugger so that you can visually see which routes are being matched. The one I use is Phil Haack's, but there are others available:
Install-Package RouteDebugger
Separately, don't you need a name parameter on your search action to match this route?

ASP.NET Membership.FindUsersByName not working. Can't search for users. 404's

When I use this action and go to Profile/Username, it gives me a 404 even though the name exists. I've used Membership.GetNumberOfUsersOnline().ToString(); and that works just fine, returning the amount of users online correctly. I understand that if this code worked properly, it would just return a basic webpage, but it's not even doing that, I get a 404. What gives? Help is greatly appreciated! :)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace MvcMicroBlog.Controllers
{
public class ProfileController : Controller
{
//
// GET: /Profile/
public ActionResult Index(string Profile)
{
Membership.FindUsersByName(Profile);
return View();
}
}
}
Unless you have a custom route defined, you should go to /Profile/?Profile=username, or you can rename your Profile parameter to id.
If you prefer the custom route approach, you can add this to your RegisterRoutes method in Global.asax.cs, before the Default route:
routes.MapRoute(
string.Empty,
"Profile/{Profile}",
new { controller = "Profile", action = "Index" }
);

Defining conditional routes

I've been searching for something similar but no luck. I want to build an app that uses different controllers for same urls. Basic idea is like that if a user is logged in as admin he uses lets say admin controller, if user is just a user he uses user controller. This is just an example, basically I want to have a function that decides what controller route takes.
Thank u everyone. Any help is greatly appreciated.
PS
Use of this:
Admin has different UI and options,
Output catching,
Separation of concern
You need to create a RouteConstraint to check the user's role, as follows:
using System;
using System.Web;
using System.Web.Routing;
namespace Examples.Extensions
{
public class MustBeAdmin : IRouteConstraint
{
public MustBeAdmin()
{ }
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
// return true if user is in Admin role
return httpContext.User.IsInRole("Admin");
}
}
}
Then, before your default route, declare a route for the Admin role, as follows:
routes.MapRoute(
"Admins", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Admin", action = "Index", id = UrlParameter.Optional }, // Parameter default
new { controller = new MustBeAdmin() } // our constraint
);
counsellorben

Resources